aeaton1843 has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: hash of hashes sort second level keys
by almut (Canon) on Feb 19, 2010 at 18:08 UTC | |
|
Re: hash of hashes sort second level keys
by kennethk (Abbot) on Feb 19, 2010 at 17:32 UTC | |
by Anonymous Monk on Mar 07, 2012 at 14:19 UTC | |
|
Re: hash of hashes sort second level keys
by rubasov (Friar) on Feb 19, 2010 at 17:49 UTC | |
|
Re: hash of hashes sort second level keys
by aeaton1843 (Acolyte) on Feb 19, 2010 at 21:33 UTC | |
by kennethk (Abbot) on Feb 19, 2010 at 22:01 UTC | |
by rubasov (Friar) on Feb 19, 2010 at 22:42 UTC | |
by aeaton1843 (Acolyte) on Feb 19, 2010 at 23:40 UTC |