> -----Original Message----- > From: Postman Pat [mailto:[EMAIL PROTECTED]] > Sent: Monday, June 24, 2002 2:46 AM > To: [EMAIL PROTECTED] > Subject: sorting a hash by value. > > > Greetings, > I would like to sort a hash by value. The hash values are > numbers. I would > like to sort this by desceding order. How would I do this?
"sort a hash" is a bit of a non-sequiter. If you want a sorted list of values, it's a simple matter of: @list = sort { $b <=> $a } values %myhash; However, if you really want a list of keys, sorted by the corresponding value (descending numerically), you need: @list = sort { $myhash{$b} <=> $myhash{$a} } keys %myhash; > I have searched > and found nothing on it. I have found lots on sorting by key > though... Hmm, you're looking in the wrong place. The simple command: perldoc -q sort Turns up this article: How do I sort a hash (optionally by value instead of key)? Which contains an example of exactly your problem. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]