我正在苦苦寻觅perl中的对象,并试图创建一个2d数组并将其存储在我的对象的散列字段中。我知道创建一个2d数组我需要一个数组引用数组,但是当我尝试这样做时,我得到这个错误:Type of arg 1 to push must be array (not hash element)
构造函数工作正常,并且set_seqs
工作正常,但我的create_matrix
子正在抛出这些错误。如何将二维数组存储在Perl中的散列中?
下面是我在做什么:
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}, []);
}
}
什么我做错了任何想法?
'推@ {$ self - > {matrix}},[]' – 2009-10-21 18:59:12
通过命令行或网络上的'perldoc perldsc'用http://perldoc.perl.org/perldsc.html 查看Data Structures Cookbook它充满了创建和访问数据结构的例子。 – daotoad 2009-10-21 19:29:25