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

This should be indexed by the Holli Inquisition. I wonder how you figured it out. Jesuitical question: Can you explain why it probably doesn’t work with 1..0 😎 ? And didn’t you get some warnings like ...uninitalized value...? 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

Replies are listed 'Best First'.
Re^8: Why does my get_max_index function return zero? (High Water Mark Algorithm) (updated)
by AnomalousMonk (Archbishop) on Jun 10, 2019 at 22:23 UTC
    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":
    • $imax = (  1 ..   0) if $_ > $_[$imax]; for  $. = 1
    • $imax = (  0 ..   1) if $_ > $_[$imax]; for  $. = 0
    • $imax = ( 42 .. 137) if $_ > $_[$imax]; for  $. = 42
    • $imax = (137 ..  42) if $_ > $_[$imax]; for  $. = 137

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


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

      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