http://qs321.pair.com?node_id=130853


in reply to Another posible initializer
in thread Testing array of hash values

Actually,
my %p; @p{'eyes', 'hair', 'etc'} = ();
is a common idiom for using a hash slice to load a hash with keys and undef values.

Replies are listed 'Best First'.
Re: Re: Another posible initializer
by Fletch (Bishop) on Dec 11, 2001 at 08:18 UTC

    You're quite right. I was thinking more of:

    my %p; @p{ qw( a b c ) } = (1)x3;
    where you're initializing to a given value (like 1 or "on") rather than undef.

      Yes indeedy. So then, to complete this exercise and return to your very legitimate concern above about maintaining this code if the number of arguments changes, we can use:
      my %p; my @keys = qw( a b c ); @p{@keys} = (1) x @keys;
      and slice assignment will take care of itself if we change the key list.