Reputation: 25373
I have an array of anonymous hashes like this:
my @arrayOfHashes=(
{
name => 'foo',
value => ['one', 'two']
},
{
name => 'bar',
value => ['two', 'three']
}
);
I'm trying to iterate over the array and access the array within each hash:
foreach (@arrayOfHashes) {
print $_->{'value'} # ARRAY(0x88489f4)
}
The value that is printed above is not what I want... I want to use that array so it works like this:
print qw(one two) # onetwo
But, when I use qw like this:
my @arrayOfHashes=(
{
name => 'foo',
qw(one two)
},
{
name => 'bar',
qw(three four)
}
);
I get this error message at runtime (I am using strict mode):
Odd number of elements in anonymous hash at ...
How do I reference the "value" array within the foreach block?
Upvotes: 0
Views: 56
Reputation: 385556
So you have a reference to an array you want to dereference. The equivalent of @array
for when you have a reference is @{ $ref }
, so
print("@array\n");
print(join(', ', @array), "\n");
would be
print("@{ $_->{value} }\n");
print(join(', ', @{ $_->{value} }), "\n");
References:
Upvotes: 4