in reply to Re^7: 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)

Can you explain why it probably doesn’t work with 1..0?

Actually, it works without warnings for 1..0 or any other unequal pair of numbers (actually, it doesn't work at all per this, but at least it not-works consistently | actually, it isn't that consistent (update: see this)):

c:\@Work\Perl\monks>perl -wMstrict -le "sub get_max_index { my $imax = 0; ;; foreach (@_) { $imax = (1 .. 0) if $_ > $_[$imax]; } return $imax; } ;; $. = 1; ;; my @arr = (1,2,13,4,5); my $ans = get_max_index(@arr); print qq{i max == $ans; \@arr[$ans] == $arr[$ans]}; " i max == 2; @arr[2] == 13
(Update: Try this with  my @arr = (1,2,13,4,5, 99); as the test input to see it not-work.)

Note the  $. = 1; statement. Per Range Operators in perlop:

If either operand of scalar ".." is a constant expression, that operand is considered true if it is equal ("==") to the current input line number (the $. variable).
So the following flip-flop values and  $. initializations "work":

And yes, this sort of thing should definitely appear in some sort of index of heretical teachings, somewhere.


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

  • Comment on Re^8: Why does my get_max_index function return zero? (High Water Mark Algorithm) (updated)
  • Select or Download Code

Replies are listed 'Best First'.
Re^9: Why does my get_max_index function return zero? (High Water Mark Algorithm) (updated)
by karlgoethebier (Abbot) on Jun 11, 2019 at 13:26 UTC

    Thank you very much AnomalousMonk for the nice explanation and for doing other peoples work. Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help