> Date: Wed, 10 Apr 2002 17:00:41 -0400 > From: Yanick <[EMAIL PROTECTED]> > > On Wed, Apr 10, 2002 at 02:48:54PM -0400, Karger, Amir wrote: > % perl -wle '@a=qw(e d c b a); for(map $_, @a){s/./uc($&)/e};print @a' > edcba > > BUT > > % perl -wle '@a=qw(e d c b a); for(grep $_, @a){s/./uc($&)/e};print @a' > EDCBA > > As that Halmet dude said so long ago, there /is/ something > rotten in this realm of Denmark...
That is not so weird. Map returns the list of results of the BLOCK, while grep returns those values for which the BLOCK returns true. (Though golfers will usually load that BLOCK with side effects on $_, making the end result similar). So inside the BLOCK, $_ may be a live reference to an array element --- but returning it in map must make a copy (or at least the { $_ } case hasn't been optimized yet), while getting a true value in grep can always add the reference to the result, so matter how the block looks (so that optimization is much more useful). Lars Mathiesen (U of Copenhagen CS Dep) <[EMAIL PROTECTED]> (Humour NOT marked)