我正在苦苦尋覓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