How do I store a 2d array in a hash in Perl?
-
05-07-2019 - |
Question
I am struggling through objects in perl, and am trying to create a 2d array and store it in a hash field of my object. I understand that to create a 2d array I need an array of references to arrays, but when I try to do it I get this error: Type of arg 1 to push must be array (not hash element)
The constructor works fine, and set_seqs
works fine, but my create_matrix
sub is throwing these errors.
Here is what I am doing:
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}, []);
}
}
Any idea of what I am doing wrong?
Solution
You're missing an extra set of braces when you dereference $self. Try push @{$self->{matrix}}, []
.
When in doubt (if you're not sure if you're referring to the correct value in a complicated data structure), add more braces. :) See perldoc perlreftut.
OTHER TIPS
Perl is a very expressive, language. You can do that all with the statement below.
$self->{matrix} = [ map { [ (0) x $seq2 ] } 1..$seq1 ];
Is this golf? Maybe, but it also avoids mucking with the finicky push
prototype. I explode the statement below:
$self->{matrix} = [ # we want an array reference
map { # create a derivative list from the list you will pass it
[ (0) x $seq2 ] # another array reference, using the *repeat* operator
# in it's list form, thus creating a list of 0's as
# long as the value given by $seq2, to fill out the
# reference's values.
}
1..$seq1 # we're not using the indexes as anything more than
# control, so, use them base-1.
]; # a completed array of arrays.
I have a standard subroutine to make tables:
sub make_matrix {
my ( $dim1, $dim2 ) = @_;
my @table = map { [ ( 0 ) x $dim2 ] } 1..$dim1;
return wantarray? @table : \@table;
}
And here's a more generalized array-of-arrays function:
sub multidimensional_array {
my $dim = shift;
return [ ( 0 ) x $dim ] unless @_; # edge case
my @table = map { scalar multidimensional_array( @_ ) } 1..$dim;
return wantarray ? @table : \@table;
}
sub create_matrix {
my($self,$seq1,$seq2) = @_;
$self->set_seqs($seq2, $seq2);
#create the 2d array of scores
#to create a matrix:
#create a 2d array of length [$seq1][$seq2]
for( 1..$seq1 ){
push @{$self->{matrix}}, [ (undef) x $seq2 ];
}
}