in reply to max "value " of an hash
Just save both the key and the value each time you find a new maximum, instead of just the value. Something like this:
# leave $vMax undefined initially so we don't need to # guess at the lowest value; this also lets us know we # never actually looked for a maximum on the off-chance # that our hash is empty my ($kMax,$vMax); while(my ($k,$v) = each(%someHash)) { # if this is the first key,value pair, initialize $kMax,$vMax # otherwise only reset on a new maximum if (!defined($vMax) || ($v > $vMax)) { $kMax = $k; $vMax = $v; } } if (!defined($vMax)) { print "No entries in my hash table!\n"; } else { print "key at maximum=$kMax, maximum value=$vMax\n"; }
|
|---|