I am having trouble with my array of references which point to another array. Here’s a snippet of my code:
# @bah is a local variable array that's been populated, @foo is also initialized as a global variable
$foo[9] = \@bah;
# this works perfectly, printing the first element of the array @bah
print $foo[9][0]."\n";
# this does not work, nothing gets printed
foreach (@$foo[9]) {
print $_."\n";
}
Always
use strict;anduse warnings;.The
@dereference takes precedence, so@$foo[9]expects$footo be an array reference and gets element 9 from that array. You want@{$foo[9]}.use strictwould have alerted you that$foowas being used, not@foo.For some easily memorizable rules for dereferencing, see http://perlmonks.org/?node=References+quick+reference.