http://qs321.pair.com?node_id=196787


in reply to sorting a file

Assuming the list of names is small enough to fit in memory, this should work:
my @list; while(<>) { chomp; my ($first, $last) = split(/\s+/, $_); push @list, "$last, $first"; } foreach my $line( sort @list) { print $line,"\n"; }
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.