On 2011-01-12 18:08, Uri Guttman wrote:
Ruud writes:
On 2011-01-11 11:44, Uri Guttman wrote:
these are equivilent:
my @out = grep /foo/, @in ;
my @out = map { /foo/ ? $_ : () } @in ;
Indeed equivalent. There is a difference though
in what comes out of map and grep:
perl -wle'
my @in = 1 .. 4;
print "@in";
++$_ for map { $_ % 2 ? $_ : () } @in; # copies
print "@in";
++$_ for grep { $_ % 2 } @in; # aliases
print "@in";
'
1 2 3 4
1 2 3 4
2 2 4 4
interesting and evil. my rule is grep/map should never do side effects
and do read only work. if you want modifications use a proper loop.
With map and grep I prefer a functional coding style too. Still it helps
to know implementation details like aliasing.
--
Ruud
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/