http://qs321.pair.com?node_id=1214721


in reply to Abusing Map

On the other hand, if you would prefer concise and efficient, use:

my $i = $#a; $b[ $i ] = $a[ $i ] + $a[ --$i ] while $i;

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
In the absence of evidence, opinion is indistinguishable from prejudice. Suck that fhit

Replies are listed 'Best First'.
Re^2: Abusing Map
by AnomalousMonk (Archbishop) on May 17, 2018 at 13:20 UTC
    $b[ $i ] = $a[ $i ] + $a[ --$i ] while $i;

    Except that crashes with a "Modification of non-creatable array value attempted ..." fatality (after a couple of warnings) for an empty input array. I've only visually examined them, but all the preceeding for-loop and map (and even grep!) solutions seem as if they would handle empty arrays gracefully. But
        $b[ $i ] = $a[ $i ] + $a[ --$i ] while $i > 0;
    avoids the problem.


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

      fatality (after a couple of warnings) for an empty input array

      So don't do that :)

      Test before entering the loop:

      ( my $i = $#a ) > 0 or die; $b[ $i ] = $a[ $i ] + $a[ --$i ] while $i;

      Never do in a loop, what can be done outside of it.

      Wouldn't your version fail for an array with one element?


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
      In the absence of evidence, opinion is indistinguishable from prejudice. Suck that fhit
        Never do in a loop, what can be done outside of it.

        I agree, and that's why I like, in particular, the for-loop and, less so, map (and let's just pass over the grep) versions that iterate over ranges of 1 .. $#array or 0 .. $#array-1 and so will never enter the loop. (Again, I haven't actually tested all the preceding code in this thread, only stared at it, but it looks ok.)

        Wouldn't your version fail for an array with one element?

        That depends on the meaning of "fail."

        c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my @ra = (1); my $i = $#ra; ;; my @rb; $rb[ $i ] = $ra[ $i ] + $ra[ --$i ] while $i > 0; dd \@rb " []
        But maybe an operation that iterates pairwise over an array with less than a pair of elements should throw an exception or at least issue a warning. Who can say? Only the Great Specificator above.


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

Re^2: Abusing Map
by Eily (Monsignor) on May 17, 2018 at 16:16 UTC

    Doesn't that rely on undefined behaviour though? Auto increment and Auto decrement:

    Note that just as in C, Perl doesn't define when the variable is incremented or decremented. You just know it will be done sometime before or after the value is returned.
    ie: the doc doesn't rule out your code being interpreted as eval { --$i; $b[ $i ] = $a[ $i ] + $a[ $i ] } while $i;

    For example: $b[ $i ] = [ $i, --$i ] while $i; builds a list of identical pairs.</c>

    Correct and efficient might be:

    $#b = $#a-1; # Allocate a big enough array $i = @b; $b[ $i ] = $a[ $i ] - $a[ $i+1 ] while $i--;

      Doesn't that rely on undefined behaviour though? ... ie: the doc doesn't rule out your code being interpreted as ...

      Yep. And in 16 years, the behaviour, 'defined by the implementation', hasn't changed; and in the remotely possible circumstance it does, I'll fix it then.

      (Assuming any of my code ever gets run on anything newer than 5.16; and then probably by moving it to Julia.)


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
      In the absence of evidence, opinion is indistinguishable from prejudice. Suck that fhit
      div class=