note
brian_d_foy
<p>
Not that there is anything wrong with the other solutions, but here is another one. I sort the keys by values, then use a while loop to figure out where the highest value ends in the list of sorted keys, @keys. Once I know that, I slice @keys to get the right keys.
</p>
<code>
#!/usr/bin/perl
my %h = (
red => 2,
pink => 1,
orange => 4,
black => 3,
blue => 4,
green => 3,
);
my $i = 0;
my @keys = sort { $h{$b} <=> $h{$a} } keys %h;
1 while( $h{$keys[++$i]} == $h{$keys[0]} );
my @largest = @keys[0..$i-1];
print qq|Largest are "@largest"\n|;
</code>
<p>
I had a solution that used grep, but that's stupid since I don't need to go through the rest of the elements once I know I've seen the highest ones.
</p>
<code>
#!/usr/bin/perl
use strict;
my %h = (
red => 2,
pink => 1,
orange => 4,
black => 3,
blue => 4,
);
my @keys = sort { $h{$b} <=> $h{$a} } keys %h;
my @largest = grep { $h{$_} == $h{$keys[0]} } @keys;
print qq|Largest are "@largest"\n|;
</code>
<!-- Node text goes above. Div tags should contain sig only -->
<div class="pmsig"><div class="pmsig-366986">
-- <br />
brian d foy <bdfoy@cpan.org>
</div></div>
433230
433230