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:
- Note that the default sort sorts alphabetically not numerically - I noted all your keys were numbers, so I changed the sort.
- -w and use warnings; are (nearly) redundant. In general, pick one. * w*, warnings, perllexwarn
- 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.
- If a hash key contains no spaces, it will be automatically stringified - that means the quotes in your assignments are unnnecessary.