Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Matrix Declaration / Data Structure Syntax Question

by oxirane (Initiate)
on Mar 09, 2013 at 15:39 UTC ( #1022583=perlquestion: print w/replies, xml ) 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.

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.


    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' )

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1022583]
Approved by ww
[erix]: I recognise the makings of a fine argument
[LanX]: lanx wonders ... how likely is it to talk >95% BS without intention?
[erix]: "gigantic amounts of data" is also not SQLite (imho)
talexb wonders why sqlite2 was deprecated in favour of sqlite3.
[erix]: looks like a fork, rather, no?
LanX /me /me
[erix]: /hehehe
[marto]: LanX yesterday I found out about Gish gallop tactic

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (13)
As of 2017-07-28 15:38 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (431 votes). Check out past polls.