[ACCEPTED]-How can I iterate over a Perl array reference?-reference
Accepted answer
I believe that:
$self->{myArray} returns a reference.
You want to return the array:
@{$self->{myArray}}
0
$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);
$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.
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.