Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Turning foreach into map?

by Roy Johnson (Monsignor)
on Apr 04, 2005 at 20:40 UTC ( [id://444794]=note: print w/replies, xml ) Need Help??


in reply to Turning foreach into map?

There are two syntaxes for calling map. If you are passing a block of code, there is no comma between the block and the list. If you are passing an expression, there is a comma. In this case, since it's just a string, I'd recommend the expression form.

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. In your case, you don't need any.

return map "$url/$_\n", @list;

Caution: Contents may have been coded under pressure.

Replies are listed 'Best First'.
Re^2: Turning foreach into map?
by tlm (Prior) on Apr 04, 2005 at 21:18 UTC

    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 +:

    @out = map +( ... ), @in;
    I find myself using this trick all over the place, not only with map and grep but with, e.g. print:
    print +( $foo ? $bar : $baz ) . $quux;
    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.)

    the lowliest monk

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://444794]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-25 20:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found