Arrays Perl对象中的数组数组
我试图在Perl对象中使用数组数组,但仍然不了解它是如何工作的 以下是构造函数:Arrays Perl对象中的数组数组,arrays,perl,perl-data-structures,Arrays,Perl,Perl Data Structures,我试图在Perl对象中使用数组数组,但仍然不了解它是如何工作的 以下是构造函数: sub new { my $class = shift; my $self = {}; $self->{AoA} = []; bless($self, $class); return $self; } 下面是代码中向AoA中插入内容的部分: push (@{$self->{AoA}}[$row], $stuff); 在构造函数中定义数组的过程中,我仍然找不到任何东西。您不需要在构
sub new {
my $class = shift;
my $self = {};
$self->{AoA} = [];
bless($self, $class);
return $self;
}
下面是代码中向AoA中插入内容的部分:
push (@{$self->{AoA}}[$row], $stuff);
在构造函数中定义数组的过程中,我仍然找不到任何东西。您不需要在构造函数中定义AoA,只需定义最顶层的arrayref即可。就幸运的散列而言,AoA仅仅是一个arrayref 你的构造函数是完美的 要插入,请执行两项操作:
# Make sure the row exists as an arrayref:
$self->{AoA}->[$row] ||= []; # initialize to empty array reference if not there.
# Add to existing row:
push @{ $self->{AoA}->[$row] }, $stuff;
或者,如果要添加已知的索引元素,只需
$self->{AoA}->[$row]->[$column] = $stuff;
您在执行
push@{$self->{AoA}}[$row]
时遇到的问题是您过早地取消了对数组1级别的引用。那么,除了在其中一个数组中推送内容之外,我还需要做些什么?还是我推错了方向?@claferri-updated,请检查答案是否正确。对不起,保存得太早了