Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

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 examining the Monastery: (9)
As of 2017-11-20 17:02 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (289 votes). Check out past polls.