Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I have an hash of array. I would like to be able to sort the hash based on the value of the array. But I cannot get to the array in the hash to be able to sort it. the example below does not work becaue I do not know how to pass the array into the sort routine.
code example :
my %colorhash = ( CCgray => ["0","0","0"], CCwhite => ["1","0","0"], CCgrey => ["0","2","0"], CCBlue => ["0","0","3"], ); foreach $colorname (keys %colorhash) { for my $i (0.. $#{ $colorhash{$colorname} } ){ print "[$i] = $colorhash{$colorname}[$i] "; } print "\n"; } my @light2dark = numsort (keys %colorhash); sub numsort { $colorhash{$a}[0] <=> $colorhash{$b}[0] or $colorhash{$a}[1] <=> $colorhash{$b}[1] or $colorhash{$a}[2] <=> $colorhash{$b}[2] }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: how to sort hash of array by value
by FunkyMonk (Bishop) on Oct 01, 2007 at 22:06 UTC | |
by blkperl (Initiate) on Oct 01, 2007 at 22:51 UTC | |
by FunkyMonk (Bishop) on Oct 01, 2007 at 23:23 UTC | |
|
Re: sort hash of array by value
by snopal (Pilgrim) on Oct 01, 2007 at 23:16 UTC | |
by snopal (Pilgrim) on Oct 02, 2007 at 00:00 UTC | |
|
Re: how to sort hash of array by value
by dwm042 (Priest) on Oct 02, 2007 at 14:44 UTC | |
|
Re: sort hash of array by value
by shmem (Chancellor) on Oct 01, 2007 at 23:11 UTC | |
|
Re: how to sort hash of array by value
by apl (Monsignor) on Oct 02, 2007 at 10:01 UTC |