Assuming that you are actually storing more than one value in the final array, and that you need to two level hash to do various lookups, the following approach should work for you.

First, collect all your data. Then create an array of key pairs. This array will provide the basis for any sorted orderings you may wish to create. Now you can sort the key pair array into whatever order you wish. You may want to look at the sort docs and sorting examples in perldsc for more help.

# Read in test data. while (<DATA>) { my ( $k1, $k2, $v ) = split; $hash{$k1}{$k2}[0] = $v } # Make an array of all key pairs. my @key_pairs; foreach my $k1 ( keys %hash ) { foreach my $k2 ( keys %{$hash{$k1}} ) { push @key_pairs, [$k1, $k2]; } } # Sort key pairs my @key_pairs_by_value = sort { $hash{ $b->[0] }{ $b->[1]}[0] <=> $hash{ $a->[0] }{ $a->[1]}[0] } @key_pairs; # print sorted for my $keys ( @key_pairs_by_value ){ my ($colour, $device) = @$keys; printf("\n%-55s %-55s %-40s", $colour, $device, $hash{$colour}{$device}[0] ); }


TGI says moo


In reply to Re: Sorting multi-hash values by TGI
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.