in reply to Printing a Hash Slice problem

If you don't need to keep the key around, you can do this:

sub print_zHoA { my $HoA = shift; my @values = sort { $b->[0] <=> $a->[0] } values %{$HoA}; return $values[0]; }

Note, however, that while the code is short, it isn't as efficient as it could be since it puts all the hash slices in order rather then just finding the highest.

Replies are listed 'Best First'.
Re^2: Printing a Hash Slice problem
by dorward (Curate) on Feb 28, 2005 at 17:32 UTC

    ... and then the "Duh" moment strikes and I see how to preserve the key.

    sub print_zHoA { my $ref = shift; my %HoA = %{$ref}; my @values = sort { $HoA{$b}->[0] <=> $HoA{$a}->[0] } keys %HoA; return { $values[0] => $HoA{$values[0]} }; }