Mar*_*ung 0 perl multidimensional-array
我的引用数组指向另一个数组时遇到问题.这是我的代码片段:
# @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";
}
Run Code Online (Sandbox Code Playgroud)
yst*_*sth 11
永远use strict;和use warnings;.
的@解引用优先,所以@$foo[9]希望$foo是一个数组参考,并从该数组获取元件9.你想要的@{$foo[9]}. use strict会提醒你$foo正在使用的,不是@foo.
有关解除引用的一些易于记忆的规则,请参阅http://perlmonks.org/?node=References+quick+reference.