in reply to keys of hash

with keys. Or do you mean something else? If yes, what?

Replies are listed 'Best First'.
Re^2: keys of hash
by prafull (Novice) on May 18, 2010 at 10:36 UTC
    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
      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