Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > READFILE sorting coding problem

Reply
Thread Tools

READFILE sorting coding problem

 
 
walfish@charter.net
Guest
Posts: n/a
 
      10-25-2013
I am new to Perl and trying to read a file (employee.txt) that has four columns of data. I want to read from the file, print the first and last column, sorted by the first column. The code I have so far is below but when I run it, I only get a "1 file found" message, no data prints. Your help is greatly appreciated.

#!/usr/bin/perl
$i = 0;
@names = open(READFILE, "employee.txt") || die "Couldn't open file: $!";

foreach(sort sort_names (@names))
{
m/^\S*\s*(\S*)\s*\S*\s*\d{3}\.\d{3}\.\d{4}\s*(\S*)$/;
print("$1\t$2\n");
++$i
}
print("$i records found\n");


sub sort_names
{
$a =~ m/\S*\s*(\S*)/;
$one = $1;
$b =~ m/\S*\s*(\S*)/;
$two = $1;

$two cmp $one || $b <=> $a;
}
 
Reply With Quote
 
 
 
 
Rainer Weikusat
Guest
Posts: n/a
 
      10-25-2013
http://www.velocityreviews.com/forums/(E-Mail Removed) writes:
> I am new to Perl and trying to read a file (employee.txt) that has four columns of data. I want to read from the file, print the first and last column, sorted by the first column. The code I have so far is below but when I run it, I only get a "1 file found" message, no data prints. Your help is greatly appreciated.
>
> #!/usr/bin/perl
> $i = 0;
> @names = open(READFILE, "employee.txt") || die "Couldn't open file: $!";


This should be

open(READFILE, "employee.txt") || die "Couldn't open file: $!";
@names = <READFILE>;

You want the contents of the file in @names, not the return value of
open.
 
Reply With Quote
 
 
 
 
$Bill
Guest
Posts: n/a
 
      10-25-2013
On 10/25/2013 07:11, (E-Mail Removed) wrote:
> I am new to Perl and trying to read a file (employee.txt) that has four columns of data. I want to read from the file, print the first and last column, sorted by the first column. The code I have so far is below but when I run it, I only get a "1 file found" message, no data prints. Your help is greatly appreciated.


Something like this should work:

use strict;
use warnings;

open READFILE, "employee.txt" or die "open employee.txt: $! ($^E)";
my @names = <READFILE>;
chomp @names;
close READFILE;

printf "records found = %s\n", scalar @names;

foreach (sort { (split ' ', $a)[0] cmp (split ' ', $b)[0] } @names) {
print "$i: $_\n" if $debug;
printf "%s\t%s\n", (split ' ', $_)[0,3];
}
exit;

__END__

 
Reply With Quote
 
George Mpouras
Guest
Posts: n/a
 
      10-27-2013
#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
#open DATA, '<:utf8', 'somefile.csv' or die "Could not read fiel because
\"$^E\"\n";

my $column_to_sort = 1;
my @data;

while (<DATA>) {
/^\s*(\S*)[,\s]*(\S*?)[,\s]*(\d{3}\.\d{3}\.\d{4})[,\s]*(\S*?)\s*$/ or next;
push @data, [$1,$2,$3,$4];
}

foreach (sort {$a->[0] cmp $b->[0] || $a->[3] cmp $b->[3]} @data) {
say "@{$_}"
}

__DATA__
name2 , colum1, 444.444.4444, info4
name1 , colum3, 111.111.1111, info2
name1 , colum2, 222.222.2222, info1
name3 , colum4, 333.333.3333, info3
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ReadFile Problem Spacetech C++ 2 05-20-2006 07:42 PM
In file parsing, taking the first few characters of a text file after a readfile or streamreader file read... .Net Sports ASP .Net 11 01-17-2006 12:44 AM
Grabbing the first characters of a textfile using StreamReader or ReadFile in vb.net .Net Sports ASP .Net 0 01-11-2006 03:20 PM
Help with ReadFile() salsipius C++ 2 10-12-2005 07:10 AM
applet cannot readfile on its server :( charly Java 5 04-23-2004 05:20 PM



Advertisments