in reply to index of the minimum element of the array
which is equivalent to the cheaperuse List::Util qw( reduce ); sub min_idx { my ($ar) = @_; return reduce { $ar->[$b] > $ar->[$a] ? $b : $a } 0..$#$ar; }
sub min_idx { my ($ar) = @_; return undef if !@$ar; my $idx = 0; for (1..$#$ar) { $idx = $_ if $arr->[$_] > $arr->[$idx]; } return $idx; }
Usage:
my $min_idx = min_index(\@a);
|
---|
In Section
Seekers of Perl Wisdom