我正在通过 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}, []);
}
}
知道我做错了什么吗?