in reply to Re: Turning foreach into map?
in thread Turning foreach into map?
Watch out if your expression is surrounded by parentheses, though, because map will think they enclose all of its arguments. You either need two sets of parens (one around your expression, and one around all of map's args), or you don't need any.
You can also get away with the single set of parens if you disambiguate it with +:
I find myself using this trick all over the place, not only with map and grep but with, e.g. print:@out = map +( ... ), @in;
Without the + above, the last expression would result in the printing of only the value of the expression in the parens. (Although, if warnings are on, perl will say something about interpreting print (...) as a function.)print +( $foo ? $bar : $baz ) . $quux;
the lowliest monk
|
---|