Arrays 在Perl中通过2D数组循环?

Arrays 在Perl中通过2D数组循环?,arrays,perl,Arrays,Perl,若我有一个2D数组,怎么可能访问循环中的整个子数组?现在我有 foreach my $row(@data){ foreach my $ind(@$row){ #perform operations on specific index } } 但理想的情况是,我正在寻找与 foreach my $row(@data){ #read row data like $row[0], which if it has the data I'm looking for

若我有一个2D数组,怎么可能访问循环中的整个子数组?现在我有

foreach my $row(@data){  
 foreach my $ind(@$row){  
  #perform operations on specific index  
 }  
}
但理想的情况是,我正在寻找与

foreach my $row(@data){  
  #read row data like $row[0], which if it has the data I'm looking for  
  #I can go ahead and access $row[3] while in the same row..
}  

我对Perl相当陌生,所以可能还不了解一些东西,但我认为“全局符号”@row“需要显式的包名”,当我试图以我想要的方式使用它时。

如果代码示例中的
$row
应该是子数组或数组引用,则必须使用间接符号来访问其元素,比如
$row->[0]
$row->[1]
,等等


出现错误的原因是因为
$row[0]
实际上意味着存在一个数组
@row
,该数组可能不存在于脚本中。

您很接近了
$row
是一个数组引用,您可以使用deference操作符
->[…]
访问其元素:

foreach my $row (@data) {
    if ($row->[0] == 42) { ... }

$row[0]
引用数组变量
@row
的一个元素,它是一个与
$row
完全不同的变量(可能未定义-
全局符号…
错误消息)。您也可以尝试此

my @ary = ( [12,13,14,15],
        [57,58,59,60,61,101],
        [67,68,69],
        [77,78,79,80,81,301,302,303]);

for (my $f = 0 ; $f < @ary ; $f++) {
    for (my $s = 0 ; $s < @{$ary[$f]} ; $s++ ) {
        print "$f , $s , $ary[$f][$s]\n";
    }
    print "\n";
}
my@ary=([12,13,14,15],
[57,58,59,60,61,101],
[67,68,69],
[77,78,79,80,81,301,302,303]);
对于(我的$f=0;$f<@ary;$f++){
对于(我的$s=0;$s<@{$ary[$f]};$s++){
打印“$f,$s,$ary[$f][$s]\n”;
}
打印“\n”;
}

虽然新答案总是受欢迎的,但如果您还解释了代码如何解决问题,那么答案将对其他人更有帮助。