Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: sorting a file

by mp (Deacon)
on Sep 10, 2002 at 19:02 UTC ( [id://196787]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://196787]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-04-19 07:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found