Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays Perl对象中的数组数组_Arrays_Perl_Perl Data Structures - Fatal编程技术网

Arrays 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); 在构造函数中定义数组的过程中,我仍然找不到任何东西。您不需要在构

我试图在Perl对象中使用数组数组,但仍然不了解它是如何工作的

以下是构造函数:

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,请检查答案是否正确。对不起,保存得太早了