http://qs321.pair.com?node_id=433244


in reply to printing largest hash value

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.

#!/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|;

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.

#!/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|;
--
brian d foy <bdfoy@cpan.org>