I am having problems sorting on keys passed the first level in a hash of hashes. I have tried many things none of which seem to work. Post after post says just sort the keys as I have it below. I have come across a couple of posts, one of which was here suggesting that after the first level the sort sorts the HASH and not the keys of the HASH. Given the following code how do I sort the second level keys.
#!/usr/bin/perl -w use strict; use warnings; my (%sorthash,%test) = (); $sorthash{'10'}{'20'}= '1'; $sorthash{'40'}{'50'}= '4'; $sorthash{'20'}{'30'}= '2'; for my $keys1 (keys %sorthash ) { #print "keys1: $keys1\t"; %test = %{$sorthash{$keys1}}; for my $testkeys (sort keys %test) { print "keys: $keys1\t$testkeys\t"; print "values: $test{$testkeys}\n"; } }
This outputs: keys: 40 50 values: 4 keys: 10 20 values: 1 keys: 20 30 values: 2 instead of: keys: 10 20 values: 1 keys: 20 30 values: 2 keys: 40 50 values: 4
In reply to hash of hashes sort second level keys by aeaton1843
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |