>>>>> "R" == Ruud  <rvtol+use...@isolution.nl> writes:

  R> On 2011-01-11 11:44, Uri Guttman wrote:
  >> these are equivilent:
  >> 
  >> my @out = grep /foo/, @in ;
  >> my @out = map { /foo/ ? $_ : () } @in ;

  R> Indeed equivalent. There is a difference though
  R> in what comes out of map and grep:

  R> perl -wle'
  R>     my @in = 1 .. 4;
  R>     print "@in";

  R>     ++$_ for map { $_ % 2 ? $_ : () } @in;  # copies
  R>     print "@in";

  R>     ++$_ for grep { $_ % 2 } @in;  # aliases
  R>     print "@in";
  R> '

  R> 1 2 3 4
  R> 1 2 3 4
  R> 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.

uri

-- 
Uri Guttman  ------  u...@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to