in reply to Re: Fence vs. Posts
in thread Fence vs. Posts

I like your suggestion of the 'for modifier'. Note however, that it requires the 'push' function to do the same thing as map.
C:\Users\Bill\forums\monks>type rodinski.pl use strict; use warnings; use Data::Dumper; my @arry = qw(A B C); my @gaps; push @gaps, [$arry[$_], $arry[$_+1]] for 0..$#arry-1; print Dumper(\@gaps); C:\Users\Bill\forums\monks>perl rodinski.pl $VAR1 = [ [ 'A', 'B' ], [ 'B', 'C' ] ];
Bill

Replies are listed 'Best First'.
Re^3: Fence vs. Posts
by haukex (Archbishop) on Jun 10, 2018 at 09:55 UTC

    Sorry, I realize now that putting those two sentences next to each other probably made my post a bit confusing. When I said "using map purely for its side-effects", I was referring to these lines in the OP's code:

    map { printf "%15s to %-15s\n", $_->[0],$_->[1] } pairs @legs; map { if ($info{$_->[0]}{state} ne $info{$_->[1]}{state}) {say "Mann +Act!"} } pairs @legs; map { $info{$_[0]}{"state"}=$_[1] } pairs mesh @trip, @states;

    The return value of map is completely ignored, and instead actions are taken inside the code block that have side effects. These are the cases where I think a for statement modifier would be better.

    On the other hand, the code that I showed, map {@arr[$_,$_+1]} 0..$#arr-1 does not make any modifications to the @arr, and I do use the return value - so this is a case where I would perfer map over for.