Welcome to WebmasterWorld Guest from 50.16.24.12

Forum Moderators: coopster & jatar k & phranque

An easier way to find the highest hash value

without looping through all of them

   
8:04 am on May 26, 2006 (gmt 0)

10+ Year Member



Is there an easy way of finding out which key in my hash has the highest value. I'm currently going through each one by one and comparing them.

I know there are lots of Perl tricks and wonder if there is one for this.

As an example, say:

$hasharray{"turnips"} = 9
$hasharray{"swedes"} = 3
$hasharray{"carrots"} = 95

What is the easier way to find carrots?

Cheers

10:04 pm on May 26, 2006 (gmt 0)

5+ Year Member



you will have to go through all the hash keys to find the highest value, that is a given. But you can use sort to do the dirty work:


my @sorted = sort {$hasharray{$b} <=> $hasharray{$a}} keys %hasharray;
print $sorted[0];
 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved