[ACCEPTED]-How can I iterate over a Perl array reference?-reference

Accepted answer
Score: 33

I believe that:

$self->{myArray} returns a reference.

You want to return the array:



Score: 11

$self->{myArray} is an array reference. You need to dereference 4 it.

my @myArray = @{ $self->{myArray} };

In situations like this, the Data::Dumper module 3 is very helpful. For example, if @myArray were not 2 behaving as expected, you could run this 1 code to reveal the problem.

use Data::Dumper;
print Dumper(\@myArray);
Score: 7

$self->{myArray} is an array reference, not an array - you 3 can't store actual arrays inside a hash, only 2 references. Try this:

my $myArray = $self->{myArray};
for my $foo (@$myArray){
   # do something with $foo

You also may want to 1 have a look at perldoc perlref.

More Related questions