missingthepoint has asked for the wisdom of the Perl Monks concerning the following question:

Greetings, monks.

Let's say I have:

my %foo = map( $_ => 2*$_, 1..10 ); use Data::Dumper; print Dumper(\%foo);

... and I (naively) expect output like:

$VAR1 = { '6' => 12, '3' => 6, '7' => 14, '9' => 18, '2' => 4, '8' => 16, '1' => 2, '4' => 8, '10' => 20, '5' => 10 };

But that's not the result. It looks like it's being parsed as:

my %foo = map( $_, 2 * $_, 1..10 )

... where all but the first argument are flattened into a single list.

Why doesn't Perl DWIM here? In general terms, what's an EXPR and what's not (and why can't $_ => 2*$_ be one)?

Cheers,


Life is denied by lack of attention,
whether it be to cleaning windows
or trying to write a masterpiece...
-- Nadia Boulanger

Replies are listed 'Best First'.
Re: map EXPR - what's an EXPR?
by moritz (Cardinal) on Nov 28, 2008 at 12:08 UTC
    The problem is that => is just a comma, and if the lhs is a bare word it's automatically quoted.

    So what you write is parse as $_, 2 * $_, 1..10, so 2 * $_ is the first item of the list.

    $ perl -MO=Deparse -e 'my %foo = map( $_ => 2*$_, 1..10 );' my(%foo) = map($_, 2 * $_, 1..10);

    In general terms an EXPR is everything that returns a value, but EXPR, LIST means that there has to be a decision where EXPR ends and LIST starts, and that's the first comma.

Re: map EXPR - what's an EXPR?
by johngg (Canon) on Nov 28, 2008 at 15:05 UTC

    map says that both EXPR and BLOCK are evaluated in list context. Using a parenthetical list is ambiguous as they are taken as enclosing the arguments to map I think.

    $ perl -le ' > %h = map ( $_, 2 * $_ ), 1 .. 3; > print qq{$_ - $h{$_}} for sort {$a <=> $b} keys %h;' 0 - $

    Using an anonymous array emits a single scalar containing the reference so you then need another map to dereference it.

    $ perl -le ' %h = map @$_, map [ $_, 2 * $_ ], 1 .. 3; print qq{$_ - $h{$_}} for sort {$a <=> $b} keys %h;' 1 - 2 2 - 4 3 - 6 $

    You could construct a subroutine that returns a list in the fly.

    $ perl -le ' %h = map sub { return ( $_, 2 * $_ ) }->( $_ ), 1 .. 3; print qq{$_ - $h{$_}} for sort {$a <=> $b} keys %h;' 1 - 2 2 - 4 3 - 6 $

    Simplest is to use map BLOCK list as already suggested.

    $ perl -le ' %h = map { $_, 2 * $_ } 1 .. 3; print qq{$_ - $h{$_}} for sort {$a <=> $b} keys %h;' 1 - 2 2 - 4 3 - 6 $

    I hope this is of interest.

    Cheers,

    JohnGG

    Update: Couldn't see the wood for the parentheses, just add an extra pair so there's no ambiguity.

    $ perl -le ' %h = map ( ( $_, 2 * $_ ), 1 .. 3 ); print qq{$_ - $h{$_}} for sort {$a <=> $b} keys %h;' 1 - 2 2 - 4 3 - 6 $

    Update 2: Another way is to use a do block.

    $ perl -le ' > %h = map do { ( $_, 2 * $_ ) }, 1 .. 3; > print qq{$_ - $h{$_}} for sort {$a <=> $b} keys %h;' 1 - 2 2 - 4 3 - 6 $
Re: map EXPR - what's an EXPR?
by ikegami (Patriarch) on Nov 28, 2008 at 17:32 UTC

    First some solutions, then I'll explain.

    map(($_ => 2*$_), @list) map +($_ => 2*$_), @list map { $_ => 2*$_ } @list

    In general terms, what's an EXPR and what's not (and why can't $_ => 2*$_ be one)?

    You gotta keep in mind the entire list of arguments is also an expression (a list operator), so the expression is bounded by a comma. "=>" is a fancy form of the comma operator.

    So why you have is basically a precedence issue. And when you have a precedence issue, you use parens.

    map(($_ => 2*$_), @list)

    map is often used without parens, but that's a problem here since

    map ($_ => 2*$_), @list # XXX

    means

    map(($_ => 2*$_), ()), @list # XXX

    The usual trick is to use the unary + operator.

    map +($_ => 2*$_), @list

    Omitting parens around function in Perl is frought with peril.

Re: map EXPR - what's an EXPR?
by parv (Parson) on Nov 28, 2008 at 13:24 UTC
    Try other syntax: map { EXPR } LIST.