http://www.perlmonks.org?node_id=347308

shilpam has asked for the wisdom of the Perl Monks concerning the following question:

I want to assign an array to the value of hash. The key is scalar varaible. At a later stage in program, I need to access the key-value pairs. My code is something like this:
my %new_hash; my $some_key1 = 1234; my @some_array1 = ("Name1", "Email1", "Age1"); $new_hash{$some_key1} = @some_array1; my $some_key2 = 1235; my @some_array2 = ("Name2", "Email2", "Age2"); $new_hash{$some_key2} = @some_array2; for (keys %new_hash) { my @value_array = $new_hash{$_}; print "Key is $_ and Second Element of array is $value_array[1] \ +n"; }
$value_array1 does not fetch any value...it is empty. But, $value_array[0] gets the length of the array i.e. 3. Can someone tell me the correct way to go about it?

Replies are listed 'Best First'.
Re: How to assign an array to a value in hash?
by davido (Cardinal) on Apr 22, 2004 at 15:48 UTC
    my %new_hash; my $some_key1 = 1234; my @some_array1 = qw/Name1 Email1 Age1/; $new_hash{$some_key1} = [@some_array1]; my $some_key2 = 1235; my @some_array2 = qw/Name2 Email2 Age2/; $new_hash{$some_key2} = [@some_array2]; for ( keys %new_hash ) { my @value_array = @{$new_hash{$_}}; print "Key is $_ and Second element of array is $value_array[1]\n" +; }

    You should read perlreftut, perllol, perlref, and perldsc... it'll take you a couple hours, but it will really help your understanding of references.


    Dave

      I know this is old, but the @value_array in the for-loop doesn't need to be created.

      I'm sure there's a lot more that could be cleaned up to make this closer to a one-liner (just looking at it), but I figured I'd update the most important part, due to efficiency issues with creating temporary variables in a loop.

      You can just reference an array's value like: $new_hash{$key}[0]

      Note: The example uses $_ instead of $key and instead of choosing the second element in the array (1), I've replaced with the first element (0).
      This hits the key point.
Re: How to assign an array to a value in hash?
by matija (Priest) on Apr 22, 2004 at 12:00 UTC
    When you say $new_hash{$some_key1} = @some_array1; you are assigning to a scalar. Try
    @{$new_hash{$some_key1}} = @some_array1; # extra {} are for clarity

    Of course, you will need to change the @value_array assignement:

    my @value_array = @{$new_hash{$_}};
Re: How to assign an array to a value in hash?
by periapt (Hermit) on Apr 22, 2004 at 12:33 UTC
    Your problem occurs in the mixed use of array and scalar values. The assignment $new_hash{$some_key1} = @some_array1 actually references @some_array1 in the scalar context (because $new_hash{$some_key1} is of the scalar data type). In the scalar context @some_array1 will simply return the number of elements in the array. If you want to save the actual array, assign a reference to the array into the hash such as $new_hash{$some_key1} = \@some_array1. This will preserve the array. You can now reference the elements of the array as $new_hash{$some_key1}[0], $new_hash{$some_key1}[1] etc

    PJ
Re: How to assign an array to a value in hash?
by Fletch (Bishop) on Apr 22, 2004 at 12:41 UTC

    perldoc perlreftut, perldoc perldsc, perldoc perllol.