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

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

by Juerd (Abbot)
on Aug 05, 2003 at 07:00 UTC ( #280900=note: print w/ replies, xml ) Need Help??


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

@seen{@list}=();

A bit faster:

undef @seen{@list};

Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }


Comment on Re: Re: Perl Idioms Explained - keys %{{map{$_=>1}@list}}
Download Code
Re^3: Perl Idioms Explained - keys %{{map{$_=>1}@list}}
by Roy Johnson (Monsignor) on Feb 28, 2005 at 23:06 UTC
    undef is not documented to work on slices. That command should probably generate at least a warning.

    Caution: Contents may have been coded under pressure.
      perlfunc's entry for undef suggests this behavior on slices, if one squints:
      Undefines the value of EXPR, which must be an lvalue... Saying undef $hash{$key} will probably not do what you expect...
      That is, taking "undef EXPR" as (usually) equivalent to "EXPR = undef", the behavior is sensible, even expected. At any rate, a warning seems inappropriate, as this feature is long-standing:
      $ perl -le 'undef @h{qw|a b c|}; print "$]: ", join " ", keys %h' 5.00404: a b c
        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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2014-12-20 02:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (95 votes), past polls