Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

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.

Comment on Tricky sorting of HOH.
Download Code
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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (13)
As of 2014-09-19 16:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (143 votes), past polls