Contributed by George_Sherston
on Jan 13, 2002 at 02:32 UTC
Q&A
> hashes
Answer: How do I sort a hash by its values? contributed by tye 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:
my $key= @array[2];
my $value= $hash{$key};
instead of, using your data structure:
my $key= $array_of_hashes[2]{key};
my $value= $array_of_hashes[2]{value};
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. | Answer: How do I sort a hash by its values? contributed by particle you can read about this in perlman:perlfaq4. additionally, you can search here for 'sort hash values' or something similar. a little legwork goes a long way.
~Particle | Answer: How do I sort a hash by its values? contributed by George_Sherston Warning: I'm the person who asked the question!
How I'd do it is
my %hash = (
foo => 2,
bar => 1,
baz => 3,
bun => 2,
);
my @array = sort {$hash{$a} <=> $hash{$b}} keys %hash;
Of course, this array only has the hash keys in it, albeit correctly sorted. If you want a data structure with both keys *and* values from %hash then you have to choose a data structure that meets your needs. A straight hash is no good, because you have duplicate values, so you'll lose some key / value pairs when you reverse them. An array of hashes is one choice:
my @array_of_hashes;
push @array_of_hashes, {key => $_, value => $hash{$_}} for @sorted;
And to see what that looks like,
use Data::Dumper;
print Dumper(\@array_of_hashes);
Gets you
$VAR1 = [
{
'value' => 1,
'key' => 'bar'
},
{
'value' => 2,
'key' => 'bun'
},
{
'value' => 2,
'key' => 'foo'
},
{
'value' => 3,
'key' => 'baz'
}
];
... but I'm interested to see what other monks come up with: it was my slight unease with this solution that prompted me to post this question. |
Please (register and) log in if you wish to add an answer
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Outside of code tags, you may need to use entities for some characters:
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|