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 (Abbot) 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 );