in reply to Re^2: Why does my get_max_index function return zero? (High Water Mark Algorithm)
in thread Why does my get_max_index function return zero? (High Water Mark Algorithm)

Please see my edited code above

Please have a look at How do I change/delete my post?, in particular, "It is uncool to update a node in a way that renders replies confusing or meaningless". But luckily I still had your original code sitting around:

sub get_max_index { my $i, $imax; for ($i=0; $i<@_; $i++) { $imax=$i if ($_[$i] > $_[$imax]); } return $imax; } my @arr = (1..10); my $ans = get_max_index(@arr);

Other than that, Paladin has already pointed out the issue with the second my declaration inside the loop in your updated code. I just wanted to add that in general, one should not combine my with an if statement modifier, as its behavior is undefined (see Statement Modifiers).