laziness, impatience, and hubris 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?
 [Corion]: Eily: Sure, that's what I do, but some things you need to do on the Android device directly, like when navigating the bootloader :-) [Eily]: marto you just need a longer cable :P [Corion]: marto: Yeah, but I'm somewhat wary of installing random USB drivers downloaded from mega.nz , Google Drive or whatever, so Linux wins there due to there being no conflicts and me just having to edit one text file in the worst case, to add the USB vendor [hippo]: Long USB cable FTW. [Corion]: Eily: I've thought of that, but I don't like running long cables through the appartment because sooner or later I'll trip over it, pulling at least one device off its stand :) [marto]: hmm, may have to patch CPAN::Meta to move from search.cpan to metacpan in the META.json/yml files [Corion]: marto: Heh - it seems that they plan to keep the search.cpan.org links alive for a long time. But still, I plan on moving PM to use/generate the new links [Corion]: And I think it's better to generate links to the new world instead of keeping the older links alive by generating new versions of them ;) [marto]: yeah, I guess it's supposed to be a permanent redirect, but better to make the move where possible

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (12)
As of 2018-05-23 09:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
World peace can best be achieved by:

Results (166 votes). Check out past polls.

Notices?