Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Answer: How do I sort a hash by its values?

by tye (Sage)
on Jan 13, 2002 at 04:24 UTC ( #138361=categorized answer: print w/replies, xml ) Need Help??

Q&A > hashes > How do I sort a hash by its values? - Answer 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
[Lady_Aleena]: Your Mother, how would that work? I'm having a hard time understanding the pod.
[Your Mother]: $dir->chidren, grep {} $file->slurp &c &c. :P
[Your Mother]: Stick with what you know. Or post a SOPW and if it's not complicated I guarantee someone will answer; me if it's straightforward and no one else gets it better first.
[1nickt]: ++YourMother (Grammar Vigilante of Bristol)
[Lady_Aleena]: Um, how does grep do multiline? print "$file:$_" if grep { /get_(array|hash| data)\(.+\)/ } $_; only returns the same 18 lines the command line grep does.

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (11)
As of 2017-05-23 19:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?