Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

need help in creating multi dimensional array

by uva (Sexton)
on Feb 10, 2006 at 10:42 UTC ( [id://529318]=perlquestion: print w/replies, xml ) Need Help??

uva has asked for the wisdom of the Perl Monks concerning the following question:

how to create multi dimensional arrays in perl.since what ever we give, like
$array[1] [2]='help'; print $array[1][2]; and $peanut[0]->[0]->[0] ='cracked'; print $peanut[0]->[0]->[0] ;
,it takes and print the value. how can we limit the size eg : in c we give
"int dim[3] [4];"

Replies are listed 'Best First'.
Re: need help in creating multi dimensional array
by davorg (Chancellor) on Feb 10, 2006 at 11:44 UTC

    Perl doesn't really go in for all that restrictive nonsense. It will happily auto-vivify array elements at whatever index you give it.

    You might want to look at Tie::Array::Bounded, but note that Perl doesn't actually have multi-dimensional arrays so that you'll need to restrict each array individually.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      yep, so if the indices in your arrays are arbitrary or if your multidimensional array is sparse (like a sparse matrix) you'd better use hashes of hashes.
      for instance if you declare $a->[10000] = 1 it also autovivivies elements 0 until 9999 setting the value to undef. This takes up unnessessary (? difficult word;) space, and makes loops much more cumputation intensive.

      Cheers,
      Rob

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://529318]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2025-06-24 10:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.