Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: How can I group lines in a file?

by zwon (Abbot)
on May 17, 2009 at 07:45 UTC ( [id://764490]=note: print w/replies, xml ) Need Help??


in reply to Re: How can I group lines in a file?
in thread How can I group lines in a file?

Just want to note that you can get the same order if you sort by name length or by first value ;)

use strict; use warnings; my %res; while (<DATA>) { my ( $name, $weight ) = split /\s+/; push @{ $res{$name} }, $weight; } for ( sort { length($a) cmp length($b) } keys %res ) { print "$_ ", join( ",", @{ $res{$_} } ), "\n"; } for ( sort { $res{$a}->[0] <=> $res{$b}->[0] } keys %res ) { print "$_ ", join( ",", @{ $res{$_} } ), "\n"; } __DATA__ jim 14 john 23 ernest 38 matilda 43 jim 34 ernest 27 john 44 matilda 22

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2026-03-09 09:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.