Perl 对象中的数组数组
我正在尝试在 Perl 对象中使用数组的数组,但仍然不明白它是如何工作的。
这是构造函数:
sub new {
my $class = shift;
my $self = {};
$self->{AoA} = [];
bless($self, $class);
return $self;
}
这是向 AoA 中插入内容的代码部分:
push (@{$self->{AoA}}[$row], $stuff);
我仍然找不到在构造函数中定义数组数组的任何内容。
I'm trying to use an array of arrays in a Perl object and am still not getting how it works.
Here is the constructor:
sub new {
my $class = shift;
my $self = {};
$self->{AoA} = [];
bless($self, $class);
return $self;
}
And here is the part of code which inserts stuff into AoA:
push (@{$self->{AoA}}[$row], $stuff);
I still can't find anything on the way to define an array of arrays in the constructor.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要在构造函数中定义 AoA - 只需要最顶层的 arrayref 即可。就受祝福的哈希而言,AoA 只是一个数组引用。
你的构造函数是完美的。
要插入,您需要做两件事:
或者,如果您要添加已知索引元素,只需
执行
push @{$self->{AoA}}[$row]
的问题是您太早取消了数组 1 级别的引用。You don't need to define AoA in a constructor - merely the topmost arrayref. As far as the blessed hash is concerned, the AoA is merely an arrayref.
Your constructor is perfect.
To insert, you do 2 things:
Or, if you are adding a known-index element, simply
Your problem with doing
push @{$self->{AoA}}[$row]
was that you dereferenced the array 1 level too early.