Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: highest value in hash

by nvivek (Vicar)
on Apr 01, 2013 at 06:12 UTC ( [id://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";

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2024-04-18 12:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found