"be consistent" | |
PerlMonks |
Re: dimensioning arraysby eg (Friar) |
on Feb 16, 2001 at 15:35 UTC ( [id://58836]=note: print w/replies, xml ) | Need Help?? |
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:
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:
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".
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 Section
Seekers of Perl Wisdom
|
|