in reply to Why does my get_max_index function return zero? (High Water Mark Algorithm)

because you mask original $imax inside the loop with my $imax=$i...

Below is a more esoteric way to do it without a function. It goes under the "Schwartzian transform":

my @arr = (1,2,3,5,10,3,4,300,1,2,-2); my ($i) = map { $_->[0] } sort { $b->[1] <=> $a->[1] } map { [ $_ , $a +rr[$_] ] } 0..$#arr; print "i=$i\n"; # index 7 => value 300

starting from right to left, create an array of all the indices in your array, package the index and its value into a temporary arrayref and then into an intermediate array, sort that intermediate array with custom sort function which sorts in reverse (i.e. biggest first) wrt the second element in said packaged arrayref (which is the value). Remember index and value are still packaged together, so extract the index (first item of the arrayref) with the leftmost map which returns an array of indices of the items in your original array sorted by their value. However my ($i) = ... will retain only the first item (the index with largest value) and skip the rest.

sort of...

bw bliako

Edit: changed $#arr-1 to $#arr, thanks AnomalousMonk

Replies are listed 'Best First'.
Re^2: Why does my get_max_index function return zero? (High Water Mark Algorithm)
by AnomalousMonk (Archbishop) on Jun 03, 2019 at 21:41 UTC
    my ($i) = map { $_->[0] } sort { $b->[1] <=> $a->[1] } map { [ $_ , $arr[$_] ] } 0..$#arr-1;

    I don't understand the  0..$#arr-1 expression in the quoted statement. Did you mean  0..$#arr or  0..@arr-1 instead?

    c:\@Work\Perl\monks>perl -wMstrict -le "my @arr = (1,2,3,5,10,3,4,300,1,2,-2); my ($i) = map { $_->[0] } sort { $b->[1] <=> $a->[1] } map { [ $_ , $ +arr[$_] ] } 0..$#arr-1; print qq{i of largest == $i (\@arr[$i] == $arr[$i])}; " i of largest == 7 (@arr[7] == 300) c:\@Work\Perl\monks>perl -wMstrict -le "my @arr = (1,2,3,5,10,3,4,300,1,2,-2, 999); my ($i) = map { $_->[0] } sort { $b->[1] <=> $a->[1] } map { [ $_ , $ +arr[$_] ] } 0..$#arr-1; print qq{i of largest == $i (\@arr[$i] == $arr[$i])}; " i of largest == 7 (@arr[7] == 300)
    (I'll pass over the propriety of using an O(n log n) sort operation for what is essentially an O(n) task :)


    Give a man a fish:  <%-{-{-{-<

      thanks fixed that to $#arr

      (I'll pass over the propriety of using an O(n log n) sort operation for what is essentially an O(n) task :)

      very good point

        FWIW, here's a GRT (decorate-sort-undecorate) approach, but still O(n log n):

        c:\@Work\Perl\monks>perl -wMstrict -le "use Test::More 'no_plan'; use Test::NoWarnings; ;; use List::Util qw(shuffle); ;; use constant RA => shuffle -4 .. 4; use constant RA_MAX_I => $#{[ RA ]}; ;; for my $i_ins (0 .. RA_MAX_I+1) { my @rb = ((RA)[ 0 .. $i_ins-1 ], 99, (RA)[ $i_ins .. RA_MAX_I ]); my ($i_max) = map unpack('x[N] N', $_), reverse sort map pack('N N', 0x7fff_ffff+$rb[$_], $_), 0 .. $#rb ; ok $i_ins == $i_max, qq{largest at (@rb)[$i_ins]}; } ;; done_testing; " ok 1 - largest at (99 -1 -2 4 1 -3 2 3 0 -4)[0] ok 2 - largest at (-1 99 -2 4 1 -3 2 3 0 -4)[1] ok 3 - largest at (-1 -2 99 4 1 -3 2 3 0 -4)[2] ok 4 - largest at (-1 -2 4 99 1 -3 2 3 0 -4)[3] ok 5 - largest at (-1 -2 4 1 99 -3 2 3 0 -4)[4] ok 6 - largest at (-1 -2 4 1 -3 99 2 3 0 -4)[5] ok 7 - largest at (-1 -2 4 1 -3 2 99 3 0 -4)[6] ok 8 - largest at (-1 -2 4 1 -3 2 3 99 0 -4)[7] ok 9 - largest at (-1 -2 4 1 -3 2 3 0 99 -4)[8] ok 10 - largest at (-1 -2 4 1 -3 2 3 0 -4 99)[9] 1..10 ok 11 - no warnings 1..11


        Give a man a fish:  <%-{-{-{-<