in reply to Re^2: How to create a hash whose values are undef?
in thread How to create a hash whose values are undef?

If you want to initialise the values to some other value, say zero, for example:
my %hash; @hash{@keys} = (0) x @keys;
The LHS is a slice, on the RHS the value, 0, is in parentheses which generates a list, the @keys is used in scalar context to get the correct number of values.

Replies are listed 'Best First'.
Re^4: How to create a hash whose values are undef?
by Marshall (Canon) on Jul 06, 2009 at 12:11 UTC
    Great! I like your thinking, but you should be aware that "0" and even "" is not the same as "undef".
    #!/usr/bin/perl -w use strict; use Data::Dumper; my @keys = (1,2,3); my %hash; @hash{@keys} = (undef) x @keys; print Dumper \%hash; __END__ Prints: $VAR1 = { '1' => undef, '3' => undef, '2' => undef };
      Exactly (that's why I said "some other value").
      Otherwise it could be done thus:
      my %hash; @hash{@keys} = undef;
      No need for the x operator. This sets the first value to undef, the others are unspecified so are set to undef as well.