in reply to hash of hashes sort second level keys

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.

Replies are listed 'Best First'.
Re^2: hash of hashes sort second level keys
by Anonymous Monk on Mar 07, 2012 at 14:19 UTC

    Simply I tried this and seems to be working:-

    #!/usr/bin/perl use strict; use warnings; my ($sorthash); $sorthash->{'10'}->{'20'}= '1'; $sorthash->{'40'}->{'50'}= '4'; $sorthash->{'20'}->{'30'}= '2'; foreach my $diff (sort {$sorthash->{$b} <=> $sorthash->{$a}} keys %{$s +orthash}) { foreach my $rptNo (keys %{$sorthash->{$diff}}) { print "$diff--- $rptNo\n"; } }

    By: Kuldip Singh Behal