你要写的是什么
print "$ref->[2][3]";
或
print "@$ref[2]->[3]";
根据您的描述,我假设您已声明 @Table 是这样的:
my @Table = ([1, 2, 3, 4],
['a', 'b', 'c', 'd'],
['e', 'f', 'g', 'h'],
['i', 'j' 'k' 'l']);
也就是说,我很确定你没有使用my,因为你没有使用use strict;。我怎么知道这个?如果您使用过它,您会收到一条消息说Global symbol "@ref" requires explicit package name。您尝试使用$ref[2] 访问的是数组@ref 中的一个元素;不是数组 ref $ref 中的元素。也有可能您使用括号(( 和 ))而不是括号([ 和 ])来包围内部数组,这是一个问题,因为这会导致 Perl 将数组扁平化为
my @Table = (1, 2, 3, 4, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' 'k' 'l');
这不是你想要的。
${$ref[2][3]} 存在多个问题。首先,访问数组 ref 中元素的正确方法是$ref->[2]->[3],也可以写成$ref->[2][3](我通常避免使用这种表示法,因为我认为它不太直观)。如果你成功获取了那个元素,你会得到${"h"},这是一个问题,因为Perl 抱怨Can't use string ("h") as a SCALAR ref。
编辑:由于我回答后问题发生了很大变化,因此记录了一个适用的解决方案:
#!/usr/bin/perl
use strict;
use warnings;
my $ref = [];
open (my $fh, "<", "file.txt") or die "Unable to open file $!\n";
push @$ref, [split] for (<$fh>);
close $fh;
print $ref->[1]->[2],"\n"; # print value at second row, third column
前几天我在 SO 的另一个答案中看到了这个 Perl references quick-reference。你会受益于看看它。并且永远不要在没有use strict;use warnings; 的情况下编写 Perl 代码。这是自找麻烦。