Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: sorting a file

by mp (Deacon)
on Sep 10, 2002 at 19:02 UTC ( #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? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2022-01-28 20:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (73 votes). Check out past polls.

    Notices?