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

Hello All,

Once again i call on you for guidence.

Im after putting 5 hases into an array, so i can return it with all five hashes from a method. The 5 hashes are created in the method, which is given two strings as input. The 5 hashes contain data (doesnt matter what it is). In the original call im assigning an array to the array of hashes returned by the method. I then want to extract each hash and assign the complete hash to a new hash.
Here is a sample of my code:

#original call my @array_of_hashes = get_array_of_hashes($value1, $value2); my %probe_name = $array_of_hashes[0]; my %probe_start = $array_of_hashes[1]; my %probe_stop = $array_of_hashes[2]; my %probe_chromo = $array_of_hashes[3]; my %probe_seq = $array_of_hashes[4]; #method sub get_array_of_hashes($$) { my ($value1,$value2) = @_; my %hash_one; my %hash_two; my %hash_three; my %hash_four; my %hash_five; ## DO SOME STUFF HERE TO GET EXTRA DATA in a loop { $hash_one{$some_value} = $some_other_value; $hash_two{$some_value} = $some_other_value; $hash_three{$some_value} = $some_other_value; $hash_four{$some_value} = $some_other_valuee; $hash_five{$some_value} = $some_other_value; } push(@array_of_hashes, %hash_one); push(@array_of_hashes, %hash_two); push(@array_of_hashes, %hash_three); push(@array_of_hashes, %hash_four); push(@array_of_hashes, %hash_five); return @array_of_hashes; }

Im not sure what is wrong here, the error message I get says:

Reference found where even-sized list expected

Any thoughts ?

cheers,
MonkPaul.

Replies are listed 'Best First'.
Re: Pulling a hash from within an array
by ambrus (Abbot) on Jun 02, 2006 at 11:41 UTC

    You need to take reference of the hashes before you push them to the array, as the way you write flattens the hashes to a list:

    push(@array_of_hashes, \%hash_one);
    After that, you have to dereference the hashes when you retreive them from the array:
    my %probe_name = %{$array_of_hashes[0]};
Re: Pulling a hash from within an array
by ww (Archbishop) on Jun 02, 2006 at 17:43 UTC

    and, now that you have the fish from ambrus, learn to fish, at
          % perldoc perlref
    (from whatever your commandline looks like.)