Your issue is that the print order ends up coming from the outer for loop, which returns results based upon the order keys are returned in your outer loop. Do your first level keys always have exactly one second level key? This would make things much easier. Assuming not, you need to collect all second level keys, sort them and then search through all your hashes to find where they occur. Given this is a subroutine, I think the clearest way to do this is to cache the reversed hash results in a local hash and then crawl the results. Something like:

#!/usr/bin/perl use strict; use warnings; my %sorthash = (); $sorthash{'10'}{'20'}= '1'; $sorthash{'40'}{'50'}= '4'; $sorthash{'20'}{'30'}= '2'; # Invert the hash my %reverse_hash = (); for my $keys1 (keys %sorthash ) { my $child_hashref = $sorthash{$keys1}; for my $keys2 (keys %$child_hashref ) { $reverse_hash{$keys2}{$keys1} = $child_hashref->{$keys2}; } } # Print after sorting keys for my $keys2 (sort {$a <=> $b} keys %reverse_hash) { my $child_hashref = $reverse_hash{$keys2}; for my $keys1 (sort {$a <=> $b} keys %$child_hashref) { print "keys: $keys1\t$keys2\t"; print "values: $child_hashref->{$keys1}\n"; } }

Some side notes:

  1. Note that the default sort sorts alphabetically not numerically - I noted all your keys were numbers, so I changed the sort.
  2. -w and use warnings; are (nearly) redundant. In general, pick one. * w*, warnings, perllexwarn
  3. Given how you were using %test, it would be reasonable to scope it to the loop rather that at the script level. Scoping can give you good control over bugs and makes variable intent more clear to maintenance folks.
  4. If a hash key contains no spaces, it will be automatically stringified - that means the quotes in your assignments are unnnecessary.

In reply to Re: hash of hashes sort second level keys by kennethk
in thread hash of hashes sort second level keys by aeaton1843

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.