Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: highest value in hash

by nvivek (Vicar)
on Apr 01, 2013 at 06:12 UTC ( #1026425=note: print w/ replies, xml ) Need Help??


in reply to highest value in hash

Need to maintain both key and value in code to find which key has a largest value among all the key pairs in hash. Following is a one more way to achieve your requirement.

use strict; use warnings; use Data::Dumper; # initializing hash %hash= ( '1' => '8', '2' => '6', '3' => '3', '4' => '7'); # print the structure print Dumper \%hash; # maintaining key and value of highest value in hash # 0th index => Key # 1st index => Value my @larger =(0,0); while (my ($key, $value) = each %hash) { # compares value with each value of hash # stores key and value whenever current value is greater than prev +ious value @larger=($key, $value) if ($larger[1] < $value) ; print "$key key has a value $value \n"; } # finally prints the key which has larger value + print $larger[0]. " key has the larger value\n";


Comment on Re: highest value in hash
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1026425]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (9)
As of 2014-07-28 11:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (196 votes), past polls