READFILE sorting coding problem

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;
}
 
R

Rainer Weikusat

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.
 
$

$Bill

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__
 
G

George Mpouras

#!/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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top