Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

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?

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2015-11-28 08:59 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (740 votes), past polls