0

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?

1
  • @{$_->{'value'}}.. the error message should not occur.. since there is an even number of elements in the hashes.. for example (name, 'foo', "one", "two") Commented Jul 28, 2015 at 19:23

1 Answer 1

4

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:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.