Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Another posible initializer

by Fletch (Chancellor)
on Dec 11, 2001 at 07:32 UTC ( #130852=note: print w/replies, xml ) Need Help??


in reply to Re: Testing array of hash values
in thread Testing array of hash values

He may have been trying for:

my %p; @p{ qw( eyes hair etc ) } = ( undef ) x 3;

Of course that's crufty in that you have to hard code in the number of elements.

Replies are listed 'Best First'.
Re: Another posible initializer
by dvergin (Monsignor) on Dec 11, 2001 at 07:47 UTC
    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.

      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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2022-01-26 14:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (69 votes). Check out past polls.

    Notices?