Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^5: Perl Idioms Explained - keys %{{map{$_=>1}@list}}

by Roy Johnson (Monsignor)
on Mar 02, 2005 at 12:19 UTC ( #435789=note: print w/ replies, xml ) Need Help??


in reply to Re^4: Perl Idioms Explained - keys %{{map{$_=>1}@list}}
in thread Perl Idioms Explained - keys %{{map{$_=>1}@list}}

That is, taking "undef EXPR" as (usually) equivalent to "EXPR = undef", the behavior is sensible, even expected.
But that isn't what it does.
my @ar = (1..10); undef @ar[0..5]; print "@ar\n"; # 1 2 3 4 5 7 8 9 10 @ar[0..5] = undef; print "@ar\n"; # 7 8 9 10
The former interprets the slice as a scalar list, and undefs the last element only. The reason all the keys get defined is that it puts the slice in lvalue context, and autovivification occurs. The latter is equivalent to assigning a list to a list.

Caution: Contents may have been coded under pressure.


Comment on Re^5: Perl Idioms Explained - keys %{{map{$_=>1}@list}}
Download Code
Re^6: Perl Idioms Explained - keys %{{map{$_=>1}@list}}
by pilcrow (Sexton) on Mar 02, 2005 at 15:58 UTC
    The former interprets the slice as a scalar list, and undefs the last element only. The reason all the keys get defined is that it puts the slice in lvalue context, and autovivification occurs. The latter is equivalent to assigning a list to a list.
    I think I see your point. Would you then have expected, in nature, the following to be equivalent:
    $h{c} = undef; # 1 undef $h{c}; # 2 undef @h{qw|a b c|}; # 3
    with no. 3 taking the hash slice as a plain list, and that in a scalar context?
      I would not have expected #3's behavior. Would you? I think it merits an "undef is not a listop" warning. The correct way to undef the values is: @h{qw|a b c|} = ();
      The correct way to autovivify them but leave them unmodified if they already exist is () = \@h{qw(a b c)};

      Caution: Contents may have been coded under pressure.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (8)
As of 2014-10-31 21:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (225 votes), past polls