W
walfish
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;
}
#!/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;
}