in reply to Efficiently finding values of an extremity
tye suggested that minima be added to List::Util. It looks something like this. Of course, his minima was probably terser/cleaner :)
sub minima(&@) { my $value = shift(@_); my @matches; my $min_val; { local $_ = shift(@_); $min_val = &$value(); @matches = $_; } foreach (@_) { my $val = &$value(); if ($val == $min_val) { push(@matches, $_); } elsif ($val < $min_val) { $min_val = $val; @matches = $_; } } return @matches; }
If so, the original question can be solved with:
use List::Util qw( minima maxstr ); my $val = maxstr minima { length } @list;
and the bonus question can be solved with:
use List::Util qw( minima ); sub pick { $_[rand(@_)] } my $val = pick minima { length } @list;
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Efficiently finding values of an extremity (tye's)
by tye (Sage) on Jul 20, 2005 at 19:12 UTC | |
by blokhead (Monsignor) on Jul 20, 2005 at 22:07 UTC |