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)):
(Update: Try this with my @arr = (1,2,13,4,5, 99); as the test input to see it not-work.)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
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: <%-{-{-{-<
|
|---|
| 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 |