You'll want to read perldata, perllol and perldsc (and maybe perlref and perlreftut for good measure :)

What you'll want to do is make a reference to a list of a list (of a list of a list ... etc.) For example:

my $array_1 = [ 1, 2, 3 ]; # 1d my $array_2 = [ [1, 2, 3], [1, 2, 3], [1, 2, 3] ]; #2d my $array_3 = [ [ [1, 2, 3], [1, 2, 3], [1, 2, 3] ], [ [1, 2, 3], [1, 2, 3], [1, 2, 3] ], [ [1, 2, 3], [1, 2, 3], [1, 2, 3] ], ]; # 3d ... etc.

which can be extended to however many dimensions you want. Using an array ref makes things easy because you can just pass $array_1, $array_2, $array_3 around as you would any ol' scalar. A specific cell in your n-dimensional array can be referenced as:

$array_1->[$x]; $array_2->[$x][$y]; $array_3->[$x][$y][$z]; ... etc.

Or, by "fixed dimensions" do you mean that your n-dimensional array is of a pre-determined size? In that case you can always use the old trick of storing everything in a single array with a pre-declared length and calculating every element's position (see your favorite algorithms/data structures book for details.)

update: I forgot to mention, if your n-dimensional array is sparsely populated, you might look into using a hash and "multidimensional array emulation".

my %array = (); $array{$x1,$y1,$z1} = something; $array{$x2,$y2,$z2} = something else; ...

Obviously this is Terribly Bad if you need to loop around your array and, frankly, I've never had much use for this, but I thought I should bring it up in case it happens to work for you ...


In reply to Re: dimensioning arrays by eg
in thread dimensioning arrays by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.