in reply to index of the minimum element of the array

Neither List::Util nor List::MoreUtils export a function returning the minimums index only the minimal value.

If you don't like the classical way of a for loop checking the min and memorizing the last index you can try combining min with first_index. (note that's inefficient)

HTH! =)

update

Or do you want a sub minindex ?

Cheers Rolf

( addicted to the Perl Programming Language)

Replies are listed 'Best First'.
Re^2: index of the minimum element of the array
by Anonymous Monk on Jan 17, 2014 at 13:56 UTC
    yeah, a sub for minindex would be good. atm i am unable to patch perl on current pc, so a busrouitine for index would be the best solution for me.
      What did you try?

      Cheers Rolf

      ( addicted to the Perl Programming Language)

        Tried this:
        my $j = 1; my $p = $s[1]; #values are present starting from 1st index while ($j != $n - 1) { if (($p < $s[$j+1]) || ($p == $s[$j+1])) { }else{ $p = $s[$j+1]; } $j++; }
        i guess that $p should keep the minimum value of the array, but the question is how to get its index.

      Also take a look at List::Util for some basic examples.

      use List::Util 'reduce'; sub minindex { reduce { $_[$a] < $_[$b] ? $a : $b } 0 .. $#_ }

      Update: Less succinct now when fixed:

      use List::Util 'reduce'; sub minindex { my @x=@_; reduce { $x[$a] < $x[$b] ? $a : $b } 0..$#_ }

        You're accessing the callback's @_ when you mean to access minindex's