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

Good, now try again using foreach, but without adding a loop counter =) In the meantime I will put on my flip-flops and chill at the lake.


holli

You can lead your users to water, but alas, you cannot drown them.
  • Comment on Re^4: Why does my get_max_index function return zero? (High Water Mark Algorithm)
  • Download Code

Replies are listed 'Best First'.
Re^5: Why does my get_max_index function return zero? (High Water Mark Algorithm)
by Marshall (Canon) on Jun 04, 2019 at 22:17 UTC
    Ok, I don't see an obvious way using the flip-flop operator....Please enlighten us...

    This idea using reduce which essentially has the equivalent of a foreach index did occur to me:

    use strict; use warnings; use List::Util qw(reduce); my @arr = qw(0 3 5 12 3 2 1);; my $maxi = reduce { $arr[$a] > $arr[$b] ? $a : $b } (0..@arr-1); print "maximum i = $maxi value=$arr[$maxi]\n"; # maximum i = 3 value=12
    Oh, and yes I almost always use @array-1 instead of $#array because I tend to either forget the right characters or type it somehow wrong.
      use strict; use warnings; sub get_max_index { my $imax = 0; foreach (@_) { $imax = ($0 .. !$0) if $_ > $_[$imax]; } return $imax; } my @arr = (1,2,13,4,5); my $ans = get_max_index(@arr); print"$ans\n"; #2
      Such practices are better reserved for obfuscation purposes though, or if you want to show off.


      holli

      You can lead your users to water, but alas, you cannot drown them.
        Wow! I've looking at this and admit that I am at a loss to understand how it works. I see that it does indeed work, but even with some extra print statements, I'm not sure exactly how.

        I'd appreciate some enlightenment. ($0 .. !$0) is truly bizarre looking!
        update: How does the scalar value of the weird looking range wind up being one less when the "if statement" is satisfied as opposed to what it is before the if statement? Of course $0 is normally the file name of the executing Perl script (C:/...baha/../../this.pl) I don't understand the meaning of ($0 .. !$0).

        use strict; use warnings; $|=1; # turn of stdout buffering sub get_max_index { my $imax = 0; foreach (@_) { print "b4 if: default var = $_ imax=$imax\n"; print "b4 if: Value of weird range=".($0 .. !$0),"\n"; $imax = ($0 .. !$0), print "If triggered, new imax=$imax\n" if + $_ > $_[$imax]; print "\n"; } return $imax; } my @arr = (1,2,13,4,5); my $ans = get_max_index(@arr); print"$ans\n"; #2 __END__ b4 if: default var = 1 imax=0 b4 if: Value of weird range=1 b4 if: default var = 2 imax=0 b4 if: Value of weird range=2 If triggered, new imax=1 b4 if: default var = 13 imax=1 b4 if: Value of weird range=3 If triggered, new imax=2 b4 if: default var = 4 imax=2 b4 if: Value of weird range=4 b4 if: default var = 5 imax=2 b4 if: Value of weird range=5 2

        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