homepage Welcome to WebmasterWorld Guest from 54.211.213.10
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

    
An easier way to find the highest hash value
without looping through all of them
basenotes




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

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

 

KevinADC




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

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];

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

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