maciej has asked for the wisdom of the Perl Monks concerning the following question:
Hi, I've recently been learning a bit of perl and this site has been super helpful, so thanks to everyone who contributes.
Anyway, I've come across a problem I cannot seem to be able to figure out. I'm trying to find the coresponding key for the highest value of a hash.
My attempt is:
#!/usr/bin/perl use warnings; use strict; my %hash= ( '1', '8', '2', '6', '3','3' ,'4','7'); my $highest =0; while (my ($key, $value) = each %hash) { $highest=$key if ($highest < $value) ; print "$key key has a value $value \n"; } print "$highest key has the highest value\n";
and my results look like:
4 key has a value 7 1 key has a value 8 3 key has a value 3 2 key has a value 6 2 key has the highest value
Where as I am trying to get the last line to say "1 key has the highest value" since the value of "1" is "8"... Both keys 1 and 4 have a higher value than 2.
any tips are much appreciated.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: highest value in hash
by LanX (Saint) on Mar 31, 2013 at 03:35 UTC | |
Re: highest value in hash (key is not value)
by Anonymous Monk on Mar 31, 2013 at 02:27 UTC | |
by maciej (Initiate) on Mar 31, 2013 at 03:54 UTC | |
by Anonymous Monk on Mar 31, 2013 at 04:41 UTC | |
Re: highest value in hash
by Athanasius (Archbishop) on Mar 31, 2013 at 02:44 UTC | |
by Anonymous Monk on Mar 31, 2013 at 04:12 UTC | |
Re: highest value in hash
by hdb (Monsignor) on Apr 01, 2013 at 08:55 UTC | |
Re: highest value in hash
by nvivek (Vicar) on Apr 01, 2013 at 06:12 UTC |
Back to
Seekers of Perl Wisdom