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

deleting multiple hash keys

by cosmicperl (Chaplain)
on Feb 28, 2009 at 20:20 UTC ( [id://747205]=perlquestion: print w/replies, xml ) Need Help??

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

Hi All,
  I've come across a code technique I couldn't find documented anywhere. It's a clever way of deleting multiple hash keys:-
my %hash = ( a => 1, b => 2, c => 3 ); delete @hash{qw/a b/};
I don't fully understand why the @ is used. I guess because the qw is returning multiple values.
I was hoping someone could tell me what this is called, point me onto the right docs, or maybe even show similar clever hash techniques.


Replies are listed 'Best First'.
Re: deleting multiple hash keys
by zwon (Abbot) on Feb 28, 2009 at 20:26 UTC
      And with reference to delete, quoth the docs (emphasis added):
      delete EXPR

      Given an expression that specifies a hash element, array element, hash slice, or array slice, deletes the specified element(s) from the hash or array.

Re: deleting multiple hash keys
by Lawliet (Curate) on Mar 01, 2009 at 00:27 UTC

    As you already know, scalars are a single value and are represent by a dollar sign. This is why when accessing one value from a hash you use the dollar sign. '@' is associated with multiple values (specifically, arrays, which are multiple scalars). Thus, when accessing multiple scalars from a hash, you use the @ sign.

    I believe this is changing in Perl 6 due to the confusion it brings.

    And you didn't even know bears could type.

Re: deleting multiple hash keys
by shmem (Chancellor) on Mar 01, 2009 at 14:10 UTC
Re: deleting multiple hash keys
by cosmicperl (Chaplain) on Mar 04, 2009 at 20:02 UTC
    Thanks everyone! I'd used slices before on arrays, but didn't realise you could apply them to hashes.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://747205]
Approved by zwon
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-05-18 06:03 GMT
Find Nodes?
    Voting Booth?

    No recent polls found