Welcome to WebmasterWorld Guest from 107.20.75.63

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

An easier way to find the highest hash value

without looping through all of them

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

Junior Member

10+ Year Member

joined:Nov 4, 2003
posts:91
votes: 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

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

Junior Member

10+ Year Member

joined:July 29, 2005
posts:163
votes: 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];
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members