|Don't ask to ask, just ask|
Multidimensional Arraysby CharlesClarkson (Curate)
|on Jun 22, 2001 at 11:11 UTC||Need Help??|
Can any one explain me how to handle multidimensional array?
Well, let's just quickly review what an array is. An array is a variable that holds a list of scalar values.my @array = (1, 2, 3, 'four');
To access a single element of this array, we use a subscript. In perl a subscript is enclosed in square brackets [ ]. To print the first element of @array we write:
We use the scalar ($) prefix because we are referring to 1 value and we use a subscript of 0 because all arrays start with 0 as the first item (unless we change the value of mumble mumble mumble, which is deprecated).
Since an array can only hold a list of scalar values, it cannot hold an array (which is not a scalar) and multidimensional arrays are not possible in perl.
Let's take a short intermission here while we catch our breath.
This break is being sponsored by that wonderful little scalar - The Reference.
If you have done any assembler programming, you are familiar with indirect addressing. References are similar. (Okay, maybe not.) They allow us to refer to a scalar, array, hash, or any object indirectly.
What's more, they allow us to refer to these variables with a single scalar value. (Sort of like referring to a human as 'her' instead of 'that drop dead gorgeous blonde Gertrude Rose Bertrand'.)
Let's take a look at a reference to an array. To reference @array, we precede it with \:
Note, that if we increase the size of @array by 1000 elements, the reference stays the same:
We can refer to an element of an array reference with the arrow (->) notation:
Oh another thing, a reference refers to the original variable, if you change an element in a reference you also change it the original variable or object which is sometimes referred to as the referent.
one 2 3 four
all variables in the preceding examples
are fictitious. Any resemblance to real
variables is purely coincidental. References
will not be directly (or indirectly) liable for
any similarities to real variables.)
This preceding break was sponsored by that wonderful little scalar - The Reference.
Well, now that your finished buying your popcorn and staring at that drop dead gorgeous blonde Gertrude Rose Bertrand. Let's look at a way to have multidimensional arrays and still use perl.
You may want to read a little about references before continuing. Perlref has some stuff in it you'll find useful. (I'm far to busy to mention them here!)
Since a reference is a scalar and an array holds a list of scalars, an array could hold a list references. Now, an array reference can refer to an element of the array it refers to with the arrow notation (->) just like we did with @array before the intermission.Let's review:
And for more than one dimension:
Okay. $array refers to $reference1 and a reference to an array can use arrow notation to refer to it's elements.
$reference1-> refers to @array1's first element. Using a little algebra we can replace like terms. $reference1 = $array, so $reference1-> is the same as $array->.
(Please, take a moment to write a nice email to your under appreciated algebra teacher. We'll wait for you.)You wrote to Gertrude, didn't you? (Shame!)
Let's take a look at a reference to an array that has no name. A nameless array is (drum roll, please) anonymous. An anonymous array in perl is constructed using square brackets [ ].Named:
my $reference1 = [1, 2, 3, 'four'];
So we could rewrite:
Perl allows us to drop the -> between subscripts, so we can also write this as:
Perldsc has a section titled 'ARRAYS OF ARRAYS' that gives more examples of declaring and generating an array of array references (a.k.a. multidimensional arrays). Note that an array can contain references to arrays of differing sizes:
Data::Dumper provides a the Dumper sub which will print an array of array references. This is a great debugging tool:
Here's the output one array:
"The only dumb question is the one you fail to ask."
Update (2 FEB 2009): Fixed a typo and changed array name.
Update (5 JUN 2012): Added missing code tags around Data::Dumper code.