tweak has asked for the wisdom of the Perl Monks concerning the following question:
Hi, I don't want to spam the chatterbox anymore, but I am still stuck on how to sort a nested hash within a hash of hashes.
# here's my test hash my %hash; $hash{"Rock Bands"}{"Smashing Pumpkins"} = "5 stars"; $hash{"Rock Bands"}{"Aerosmith"} = "5 stars"; $hash{"Rock Bands"}{"Foo Fighters"} = "5 stars"; $hash{"Pop Bands"}{"Duran Duran"} = "5 stars"; $hash{"Pop Bands"}{"Blondie"} = "5 stars"; foreach my $key (sort keys %hash) { # this bit sorts ok print "KEY: $key \n"; while (my ($nestedKey, $value) = each %{ $hash{$key} } ) { # but I can't work out how to sort this iteration print "nested key: $nestedKey \n"; } print "-------\n"; }
This returns:
KEY: Pop Bands
nested key: Duran Duran
nested key: Blondie
-------
KEY: Rock Bands
nested key: Foo Fighters
nested key: Smashing Pumpkins
nested key: Aerosmith
-------
But I would like it to return:
KEY: Pop Bands
nested key: Blondie
nested key: Duran Duran
-------
KEY: Rock Bands
nested key: Aerosmith
nested key: Foo Fighters
nested key: Smashing Pumpkins
-------
Any suggestions would be welcome, thanks!!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Sorting hash of hashes
by Zaxo (Archbishop) on Jun 21, 2007 at 19:25 UTC | |
by FunkyMonk (Bishop) on Jun 21, 2007 at 19:28 UTC | |
|
Re: Sorting hash of hashes
by Util (Priest) on Jun 21, 2007 at 19:31 UTC | |
|
Re: Sorting hash of hashes
by FunkyMonk (Bishop) on Jun 21, 2007 at 19:27 UTC | |
|
Re: Sorting hash of hashes
by Herkum (Parson) on Jun 21, 2007 at 19:27 UTC |