Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

sorting HoH according to value

by sovixi (Novice)
on May 27, 2008 at 12:22 UTC ( #688655=perlquestion: print w/replies, xml ) Need Help??

sovixi has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, How can I sort HoH according to value? (in descending order) $bdry{2}{3} = 1; $bdry{3}{4} = 2; $bdry{1}{2} = 3; So at the end I have: $bdry{1}{2} = 3; $bdry{3}{4} = 2; $bdry{2}{3} = 1;

Replies are listed 'Best First'.
Re: sorting HoH according to value
by jettero (Monsignor) on May 27, 2008 at 12:32 UTC
    You almost have to build a list of all the key-pairs, if you're asking what I think you're asking.
    my @keys = sort { $bdry{ $b->[0] }{ $b->[1] } <=> $bdry{ $a->[0] }{ $a->[1] } + } map { my $k = $_; map { [$k, $_] } keys %{ $bdry{$k} } } keys %bdry; print "my greatest key-pair is: $bdry{$keys[0][0]}{$keys[0][1]}\n\n"; print "key-pair(@$_) is: $bdry{$_->[0]}{$_->[1]}\n" for @keys;

    UPDATE: I'm glad this is was exactly what you wanted. Normally I try not to answer the entire question with code, as that takes a lot of the fun out of it; but in this case there wasn't really an easier way to say it.


      that's exactly what I was looking for. Cheers
Re: sorting HoH according to value
by mwah (Hermit) on May 27, 2008 at 14:02 UTC

    of course, jettero posted already a very compact working solution, but I found the topic interesting enough to try that one too. In contrast to the solution already given, one could solve these problems per 'hash flipping'. This would trace down the hashes of hashes and at the end of the way would put the keys of the chain plus the value into an array. This array would then be a 'flipped hash representation'. This could then be handled simply by a loop:

    ... my @flipped_hash = reverse sort {$a->[0] <=> $b->[0] } flippout %bdry +; ...

    (reverse sort - to get your desired order). This may be printed via:

    ... for my $k ( @flipped_hash ) { print " \$bdry{$k->[1]}{$k->[2]} = $k->[0];\n", } ...

    which would print the desired output:

    $bdry{1}{2} = 3; $bdry{3}{4} = 2; $bdry{2}{3} = 1;

    How would such a flippout() subroutine look like? A straightforward implementation would read like:

    ... sub flippout { my %h = @_; my @f; while( my ($k,$v) = each %h ) { while( my ($vk,$vv) = each %$v) { push @f, [$vv, $k, $vk] } } return @f } ...

    This seems to be something like an explicit version of jettero's code.



      Definitely less compact, arguably more readable although I tend to read the compact stuff more easily.

      But which is faster? I know I was expecting a speedup from using each instead of keying each by hand. But does the sub slow it down?


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2021-05-17 18:37 GMT
Find Nodes?
    Voting Booth?
    Perl 7 will be out ...

    Results (159 votes). Check out past polls.