【发布时间】:2010-12-08 20:10:45
【问题描述】:
我在 perl 中的对象中苦苦挣扎,并试图创建一个二维数组并将其存储在我的对象的哈希字段中。我知道要创建一个二维数组,我需要一个数组引用数组,但是当我尝试这样做时,我得到了这个错误:Type of arg 1 to push must be array (not hash element) 构造函数工作正常,set_seqs 工作正常,但我的 create_matrix 子正在抛出这些错误。
这是我正在做的事情:
sub new {
my ($class) = @_;
my $self = {};
$self->{seq1} = undef;
$self->{seq2} = undef;
$self->{matrix} = ();
bless($self, $class);
return $self;
}
sub set_seqs {
my $self = shift;
$self->{seq1} = shift;
$self->{seq2} = shift;
print $self->{seq1};
}
sub create_matrix {
my $self = shift;
$self->set_seqs(shift, shift);
#create the 2d array of scores
#to create a matrix:
#create a 2d array of length [lengthofseq1][lengthofseq2]
for (my $i = 0; $i < length($self->{seq1}) - 1; $i++) {
#push a new array reference onto the matrix
#this line generates the error
push(@$self->{matrix}, []);
}
}
知道我做错了什么吗?
【问题讨论】:
-
push @{$self->{matrix}}, [] -
通过命令行上的
perldoc perldsc或使用perldoc.perl.org/perldsc.html 在网络上查看数据结构手册,其中包含创建和访问数据结构的示例。
标签: perl hash reference multidimensional-array perl-data-structures