No such thing as a small change PerlMonks

### Matrix Declaration / Data Structure Syntax Question

by oxirane (Initiate)
 on Mar 09, 2013 at 15:39 UTC Need Help??
oxirane has asked for the wisdom of the Perl Monks concerning the following question:

I was exploring some of the dynamic alignment programs for protein and DNA used by biologists and came across a data construct that I didn't understand...could someone explain the square bracket/square bracket/curly bracket reference construct and comment on it being a good/bad way to go about dynamic matrix declaration

```@matrix;
\$matrix[0][0]{score}   = 0;
\$matrix[0][0]{pointer} = "none";
for(\$j = 1; \$j <= length(\$seq1); \$j++) {
\$matrix[0][\$j]{score}   = 0;
\$matrix[0][\$j]{pointer} = "none";
}
for (\$i = 1; \$i <= length(\$seq2); \$i++) {
\$matrix[\$i][0]{score}   = 0;
\$matrix[\$i][0]{pointer} = "none";

Replies are listed 'Best First'.
Re: Matrix Declaration / Data Structure Syntax Question
by kcott (Chancellor) on Mar 10, 2013 at 00:49 UTC

G'day oxirane,

Welcome to the monastery.

When presented with a complex data structure, you may find Data::Dumper to be a useful tool.

Here's a simplified version of your matrix:

```\$ perl -Mstrict -Mwarnings -e '
use Data::Dumper;
my @matrix;
for my \$i (0 .. 1) {
for my \$j (0 .. 1) {
\$matrix[\$i][\$j]{row} = \$i;
\$matrix[\$i][\$j]{col} = \$j;
}
}
print Dumper \@matrix;
'
\$VAR1 = [
[
{
'col' => 0,
'row' => 0
},
{
'col' => 1,
'row' => 0
}
],
[
{
'col' => 0,
'row' => 1
},
{
'col' => 1,
'row' => 1
}
]
];

-- Ken

Re: Matrix Declaration / Data Structure Syntax Question
by bioinformatics (Friar) on Mar 09, 2013 at 17:32 UTC
It's an array of arrays (two dimensional array) with matrix[i][j] pointing to a hash with defined keys (score and pointer). Arrays of arrays (denoted AoA) and hashes of hashes (HoH) are pretty useful and I use them constantly. I'd say it was a good way to keep everything in one data structure while maintaining speed and readability of the code.

Bioinformatics
Re: Matrix Declaration / Data Structure Syntax Question
by AnomalousMonk (Chancellor) on Mar 09, 2013 at 18:51 UTC
Re: Matrix Declaration / Data Structure Syntax Question
by CountZero (Bishop) on Mar 10, 2013 at 12:11 UTC
It is neither good nor bad. It is simply the way Perl uses multi-dimensional variables.

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

My blog: Imperial Deltronics
Re: Matrix Declaration / Data Structure Syntax Question
by Anonymous Monk on Mar 09, 2013 at 15:54 UTC

Do you know the types of variables in perl? In other words, have you read perlintro/perldata/ Modern Perl ch 3?

{ 'hashes', 'are', 'curly', 'ones' }

[ 'arrays', 'are', 'square' ]

( 'lists are round' )

Create A New User
Node Status?
node history
Node Type: perlquestion [id://1022583]
Approved by ww
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2017-11-25 02:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
In order to be able to say "I know Perl", you must have:

Results (355 votes). Check out past polls.

Notices?