in reply to sorting a file
Note that I moved the sort outside of the loop. The list of names cannot be sorted until the list has been built. The first loop builds the list of names, and the second list prints it in sorted order.my @list; while(<>) { chomp; my ($first, $last) = split(/\s+/, $_); push @list, "$last, $first"; } foreach my $line( sort @list) { print $line,"\n"; }
|
---|