Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
Just another Perl shrine
 
PerlMonks  

Sorting hash of hashes by values

by winnie_pooh (Initiate)
on Nov 15, 2013 at 23:00 UTC ( #1062817=perlquestion: print w/ replies, xml ) Need Help??
winnie_pooh has asked for the wisdom of the Perl Monks concerning the following question:

#!/usr/bin/perl use strict; use warnings; my %HoH = ( 0 => { b => 2, a => 1, e => 5, c => 3, d => 4, } ); foreach my $key (sort { $HoH{0}{$a} cmp $HoH{0}{$b} } keys $HoH{0}) { my $value = $HoH{0}{$key}; print $value->{$key}, "\n"; }

In the above hash of hashes (showed just entry for $HoH{0}), I'd like to sort the inside hash by values, so that I get output like this:

1 -> a 2 -> b 3 -> c 4 -> d 5 -> e

I know the loop is wrong... any help? Thanks a lot!

Comment on Sorting hash of hashes by values
Select or Download Code
Re: Sorting hash of hashes by values
by LanX (Abbot) on Nov 15, 2013 at 23:11 UTC
    The print is wrong.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

    update

    needed time to start my laptop for a detailed answer, but you already got two good answers in the meantime... =)

Re: Sorting hash of hashes by values
by tangent (Deacon) on Nov 15, 2013 at 23:12 UTC
    You're almost there, just change the print statement to:
    print "$value -> $key\n";
    If all of the values are numeric you should change the comparison operator to <=> like so:
    foreach my $key (sort { $HoH{0}{$a} <=> $HoH{0}{$b} } keys %{$HoH{0}} +) { my $value = $HoH{0}{$key}; print "$value -> $key\n"; }
Re: Sorting hash of hashes by values
by choroba (Abbot) on Nov 15, 2013 at 23:14 UTC
    The loop is fine. Replace the print line with
    print "$key => $value\n";
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Sorting hash of hashes by values
by AnomalousMonk (Monsignor) on Nov 16, 2013 at 20:53 UTC
    foreach my $key (sort { ... } keys $HoH{0}) { ... }

    Note that keys can be passed  $HoH{0} (assuming  @HoH as given in the OP), i.e., passed a hash reference, only with Perl version 5.14+.

Re: Sorting hash of hashes by values
by AnomalousMonk (Monsignor) on Nov 16, 2013 at 21:10 UTC
    print $value->{$key}, "\n";

    But why doesn't this print statement from the OPed code work?

    Because in the  $value->{$key} expression, the  -> operator (see The Arrow Operator in perlop) expects the scalar  $value to be a reference to something, and the  {$key} part of the expression sez that that something is a hash (due to the  {} curly braces) for which the  $key scalar will be used as a key.

    However, in the  @HoH hash as defined in the OP, all the values of the hash referenced by  $HoH{0} are humble numbers like 4 or 2 and possess no referential mojo.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (14)
As of 2014-04-24 11:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (565 votes), past polls