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

I have an array with some values. I want to make elements
of an array as keys of hash. For this, ideal
process is that loop through an array and insert array
elements into keys of hash one by one.
Is it possible to make this in one line ( i mean w/o loop)?

Regards,
Prafull

Replies are listed 'Best First'.
Re^3: keys of hash
by choroba (Cardinal) on May 18, 2010 at 10:39 UTC
    Yes, it is possible:
    my %hash; @hash{@array} = ();
    This will make your list unique since a hash cannot have duplicate keys.
      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
        Try
        @hash{@array} = map 1,@a;
        It will map 1 to each member of the array.