How can I iterate over a Perl array reference?
I believe that:
$self->{myArray} returns a reference.
You want to return the array:
@{$self->{myArray}}
$self->{myArray}
is an array reference, not an array - you can't store actual arrays inside a hash, only references. Try this:
my $myArray = $self->{myArray};
for my $foo (@$myArray){
# do something with $foo
}
You also may want to have a look at perldoc perlref.
$self->{myArray}
is an array reference. You need to dereference it.
my @myArray = @{ $self->{myArray} };
In situations like this, the Data::Dumper
module is very helpful. For example, if @myArray
were not behaving as expected, you could run this code to reveal the problem.
use Data::Dumper;
print Dumper(\@myArray);