Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

sorting a file

by samoguz (Initiate)
on Sep 10, 2002 at 14:59 UTC ( [id://196696]=perlquestion: print w/replies, xml ) Need Help??

samoguz has asked for the wisdom of the Perl Monks concerning the following question:

> I am trying to sort a file of names I have to sort by last name in alphabetical order split using white space and join the last and first name with a comma. I get to work about all that except the alpabetical sorting. Could anybody please help me? am I overlooking something? I will really appreciate any help. Thank you This is my code:
while (<>){ chomp; @line = join (" * ", (split/ /)[1,0]),"\n"; @line2 = sort @line , "\n"; print @line2 . "\n"; }

Update: Added code tags by Shendal

Replies are listed 'Best First'.
Re: sorting a file
by mp (Deacon) on Sep 10, 2002 at 19:02 UTC
    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.
Re: sorting a file
by kabel (Chaplain) on Sep 11, 2002 at 07:04 UTC
    to stay on topic, here a (in my opinion) beautiful answer:
    use strict; use Tie::File; my @array; tie @array, 'Tie::File', 'test.data'; @array = sort {(split / /, $a)[1] cmp (split / /, $b)[1]} @array; untie @array;
    because of the way sort works there are _some_ more split calls, but it should work fine for short files. perhaps caching the once split person names boosts this a little bit. i noticed that using Devel::DProf breaks the code in some way?! is there a known issue?
Re: sorting a file
by Molt (Chaplain) on Sep 11, 2002 at 14:43 UTC
    Another approach to this is to use a Schwartzian Transform. Here's an example using this approach. Note that the split used here has the advantage of working on people with middle initials, and those with only a one word name. The trinary in the top map is also to deal with those with only a single name.
    #!/usr/bin/perl use warnings; use strict; # Note: Read a Schwartzian transform from # the bottom-up. It makes more sense. my @names = # Join the name back together. First name # output first, and then (And only if a # surname exists) a comma followed by the # surname. map {$_->[1]. ($_->[0]?", $_->[0]":'')} # Sort the arrays by lastname. sort {$a->[1] cmp $b->[1]} # Splits the name on the final spaces, creates # arrays with [firstname, lastname]. map {/(.*?)\s*(\S+)$/; [$1,$2]} # All of the output <>; print "'$_'\n" foreach @names;
Re: sorting a file
by Juerd (Abbot) on Sep 11, 2002 at 14:07 UTC
    Real life scenarios often don't permit just splitting to find out the first and last names. Anyway, here's my try:
    perl -le'print join ", ", @$_ for sort { $a->[0] cmp $b->[0] } map [re +verse split], <>'

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2024-04-26 02:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found