Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

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!

Replies are listed 'Best First'.
Re: Sorting hash of hashes by values
by tangent (Priest) 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 (Chancellor) 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 (Chancellor) 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.

Re: Sorting hash of hashes by values
by AnomalousMonk (Chancellor) 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 LanX (Bishop) on Nov 15, 2013 at 23:11 UTC
    The print is wrong.

    Cheers Rolf

    ( addicted to the Perl Programming Language)


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

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (1)
As of 2017-09-23 03:25 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (270 votes). Check out past polls.