Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Tricky sorting of HOH.

by tamaguchi (Pilgrim)
on Aug 25, 2006 at 13:13 UTC ( #569567=perlquestion: print w/replies, xml ) Need Help??
tamaguchi has asked for the wisdom of the Perl Monks concerning the following question:

I have the following hash:
my %hash_of_hashes=( 'A'=>{'param'=>9, 'number'=>1}, 'B'=>{'param'=>4, 'number'=>1}, 'C'=>{'param'=>5, 'number'=>1}, 'D'=>{'param'=>12, 'number'=>1},);
I need to print the thing out sorted on the value of "param" is there an easy clean way to accomplish this? Thank you for any help.

Replies are listed 'Best First'.
Re: Tricky sorting of HOH.
by holli (Monsignor) on Aug 25, 2006 at 13:19 UTC
    for ( sort { $hoh{$a}->{param} <=> $hoh{$b}->{param} } keys %hoh ) { print "$_\n"; }

    holli, /regexed monk/
Re: Tricky sorting of HOH.
by imp (Priest) on Aug 25, 2006 at 13:24 UTC
    Here's one way to do this:
    use strict; use warnings; my %hash_of_hashes=( 'A'=>{'param'=>9, 'number'=>1}, 'B'=>{'param'=>4, 'number'=>1}, 'C'=>{'param'=>5, 'number'=>1}, 'D'=>{'param'=>12, 'number'=>1}, ); for my $key (sort {$hash_of_hashes{$a}{param} <=> $hash_of_hashes{$b}{ +param}} keys %hash_of_hashes) { print "$key\n"; }
    You might also find the following useful:
Re: Tricky sorting of HOH.
by sen (Hermit) on Aug 25, 2006 at 13:32 UTC

    Hi try this one,

    print join("\n", sort { $hash_of_hashes{$a}->{param} <=> $hash_of_hash +es{$b}->{param} } keys %hash_of_hashes );

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://569567]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2017-09-25 00:19 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (275 votes). Check out past polls.