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.


Lyle

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.