in reply to Re^3: keys of hash
in thread keys of hash

Thanx , it worked .....
I have one more problem.
@array = (10,29,30,19,48,45,89,14,99,41);

my %hash;
@hash{@array} = ();

use Data::Dumper::Simple;
print Dumper(%hash);


Output of this is

%hash = (
'41' => undef,
'14' => undef,
'48' => undef,
'99' => undef,
'30' => undef,
'45' => undef,
'89' => undef,
'10' => undef,
'19' => undef,
'29' => undef
);

here , i want to keep 1 as value of each key instead of undef.

I tried this ...
@hash{@array} = ();

but i got o/p as follows ,

%hash = (
'41' => undef,
'14' => undef,
'48' => undef,
'99' => undef,
'30' => undef,
'45' => undef,
'89' => undef,
'10' => 1,
'19' => undef,
'29' => undef
);


Regards,
Prafull

Replies are listed 'Best First'.
Re^5: keys of hash
by choroba (Cardinal) on May 18, 2010 at 11:11 UTC
    Try
    @hash{@array} = map 1,@a;
    It will map 1 to each member of the array.

      Using @hash{ @array } = (1) x @array would be more idiomatic. There's no need to get map involved since you really aren't changing the values of the array at all.

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

        I usually use $hash{$_} = 1 for @array; instead.

        This is very pretty and useful. Filed.
      worked .. thanx a lot