in reply to How do I sort a hash by its values?
Don't make another structure. Just keep both the array and the hash handy. If you want all of the values (sorted), you simply do:
my @values= @hash[@array];
So, if you want the 3rd key and value, you use:
instead of, using your data structure:my $key= @array[2]; my $value= $hash{$key};
or my( $key, $value )= @{$array_of_hashes[2]}{qw( key value )}; The original array and hash also store the data using quite a bit less memory.my $key= $array_of_hashes[2]{key}; my $value= $array_of_hashes[2]{value};
|
---|
In Section
Seekers of Perl Wisdom