Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

map +

by palette (Scribe)
on Jun 21, 2005 at 09:17 UTC ( #468605=perlquestion: print w/ replies, xml ) Need Help??
palette has asked for the wisdom of the Perl Monks concerning the following question:

Hi all

I recently came across something like map + . I learnt the difference between map and map + is like, map returns a hash while map + return array of anonymous hash.

Can anyone of you can tell me in which context map and map + is used with examples ie; why we require a hash in one place and why an array of anonymous hash in another.

Thankyou

Comment on map +
Re: map +
by gellyfish (Monsignor) on Jun 21, 2005 at 09:23 UTC

    Er map + is not a separate thing to map it is simply that on occasion the plus sign (the otherwise no-op unary addition ) might be necessary to disambiguate the bare block and prevent it from being parsed as an anonymous hash.

    /J\

Re: map +
by borisz (Canon) on Jun 21, 2005 at 10:09 UTC
    The + is just to point the perl parser into the right direction see map.
    %hash = map { "\L$_", 1 } @array # perl guesses EXPR. wrong %hash = map { +"\L$_", 1 } @array # perl guesses BLOCK. right %hash = map { ("\L$_", 1) } @array # this also works %hash = map { lc($_), 1 } @array # as does this. %hash = map +( lc($_), 1 ), @array # this is EXPR and works! %hash = map ( lc($_), 1 ), @array # evaluates to (1, @array) or to force an anon hash constructor use "+{" @hashes = map +{ lc($_), 1 }, @array # EXPR, so needs , at end
    Boris
Re: map +
by tlm (Prior) on Jun 21, 2005 at 11:07 UTC

    The unary operator + is often used to disambiguate syntax that would otherwise confuse perl. In the case of map this can happen in two different situations. One is when the first non-whitespace character after the map keyword is a (. The other one is when this character is a { ...

    the lowliest monk

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://468605]
Approved by Mutant
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2014-09-20 17:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (160 votes), past polls