This was my take on the problem:

#!/usr/bin/perl use warnings; use strict; my %hash = (); my %value_hash = (); while(<DATA>) { chomp; my ($first_key, $second_key, $value ) = split /\s+/; $hash{$first_key}{$second_key} = $value; } # # Map the hash of a hash of a value into a # hash of the value containing an array of key pairs. # Then sort on the new hash. # for my $colour ( sort keys %hash ) { for my $device ( sort keys %{$hash{$colour}} ) { my $val = $hash{$colour}{$device}; printf "%-15s %-15s %5d\n", $colour, $device, $val; push @{$value_hash{ $val }} , [ $colour, $device ]; } } print "\n\nSorted Result:\n\n"; for my $val ( sort { $b <=> $a } keys %value_hash ) { for my $key_pair ( @{$value_hash{$val}} ) { printf "%-15s %-15s %5d\n", @$key_pair[0], @$key_pair[1], $val; } } __DATA__ red bike 5 red car 4 red shoes 20 yellow shoes 1 yellow skates 1000
And the output is:

C:\Code>perl multi_hash_sort.pl red bike 5 red car 4 red shoes 20 yellow shoes 1 yellow skates 1000 Sorted Result: yellow skates 1000 red shoes 20 red bike 5 red car 4 yellow shoes 1

In reply to Re: Sorting multi-hash values by dwm042
in thread Sorting multi-hash values by papai

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.