<?xml version="1.0" encoding="windows-1252"?>
<node id="90647" title="Multidimensional Arrays" created="2001-06-22 07:11:30" updated="2005-08-14 14:50:53">
<type id="956">
perltutorial</type>
<author id="85412">
CharlesClarkson</author>
<data>
<field name="doctext">
Can any one explain me how to handle multidimensional array?

&lt;P&gt;Well, let's just quickly review what an array is.
An array is a variable that holds a list of scalar values.
&lt;/P&gt;
&lt;CODE&gt;    my @array = (1, 2, 3, 'four');
&lt;/CODE&gt;
&lt;P&gt;
    To access a single element of this array, we use a
subscript. In perl a subscript is enclosed in square
brackets &lt;CODE&gt;[ ]&lt;/CODE&gt;. To print the first element of @array we
write:
&lt;/P&gt;
&lt;CODE&gt;    print $array[0];
          ^
&lt;/CODE&gt;
&lt;P&gt;
    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).
&lt;/P&gt;
&lt;P&gt;
    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.
&lt;/P&gt;
&lt;P&gt;
    Let's take a short intermission here while we catch
our breath.
&lt;/P&gt;
&lt;HR&gt;
&lt;P&gt;
    This break is being sponsored by that wonderful
little scalar - &lt;EM&gt;The Reference&lt;/EM&gt;.
&lt;/P&gt;
&lt;P&gt;
    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.
&lt;/P&gt;
&lt;P&gt;
    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'.)
&lt;/P&gt;
&lt;P&gt;
    Let's take a look at a reference to an array. To
reference @array, we precede it with \:
&lt;/P&gt;
&lt;CODE&gt;    my @array = (1, 2, 3, 'four');
    my $reference = \@array;
    print $reference;
&lt;/CODE&gt;
    prints:&lt;BR&gt;
&lt;CODE&gt;    ARRAY(0x1a6527c)
&lt;/CODE&gt;
&lt;P&gt;
    Note, that if we increase the size of @array by
1000 elements, the reference stays the same:
&lt;/P&gt;
&lt;CODE&gt;    my @array = (1, 2, 3, 'four');
    my $reference = \@array;
    print $reference;
    push @array, (1 .. 1000);
    $reference = \@array;
    print $reference;
&lt;/CODE&gt;
    prints:&lt;BR&gt;
&lt;CODE&gt;    ARRAY(0x1a6527c)ARRAY(0x1a6527c)
&lt;/CODE&gt;
&lt;P&gt;
    We can refer to an element of an array reference
with the arrow (&lt;CODE&gt;-&gt;&lt;/CODE&gt;) notation:
&lt;/P&gt;
&lt;CODE&gt;    my @array = (1, 2, 3, 'four');
    my $reference = \@array;
    print $reference-&gt;[0];
&lt;/CODE&gt;
    prints:&lt;BR&gt;
&lt;CODE&gt;    1
&lt;/CODE&gt;
&lt;P&gt;
    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 &lt;EM&gt;referent&lt;/EM&gt;.
&lt;/P&gt;
&lt;CODE&gt;    my @array = (1, 2, 3, 'four');
    my $reference = \@array;
    $reference-&gt;[0] = 'one';
    print "@array";
&lt;/CODE&gt;
    prints:&lt;BR&gt;
&lt;CODE&gt;    one 2 3 four
&lt;/CODE&gt;
&lt;SMALL&gt;
    (*disclaimer:&lt;BR&gt;
    all variables in the preceding examples&lt;BR&gt;
    are fictitious. Any resemblance to real&lt;BR&gt;
    variables is purely coincidental. References&lt;BR&gt;
    will not be directly (or indirectly) liable for&lt;BR&gt;
    any similarities to real variables.)&lt;BR&gt;
&lt;/SMALL&gt;
&lt;BR&gt;
&lt;P&gt;
    This preceding break was sponsored by that
wonderful little scalar - &lt;EM&gt;The Reference&lt;/EM&gt;.
&lt;/P&gt;
&lt;HR&gt;
&lt;P&gt;
    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.
&lt;/P&gt;
&lt;P&gt;
    You may want to read a little about references
before continuing. [perlman:perlef|Perlref] has some stuff in it you'll
find useful. (I'm far to busy to mention them here!)
&lt;/P&gt;
&lt;P&gt;
    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 (&lt;CODE&gt;-&gt;&lt;/CODE&gt;)
just like we did with @array before the intermission.
&lt;/P&gt;
    Let's review:
&lt;CODE&gt;    my @array = (1, 2, 3, 'four');
    print $array[0];
&lt;/CODE&gt;
    prints:
&lt;CODE&gt;    1
&lt;/CODE&gt;
&lt;CODE&gt;    my @array = (1, 2, 3, 'four');
    my $reference = \@array;
    print $reference-&gt;[0];
&lt;/CODE&gt;
    prints:&lt;BR&gt;
&lt;CODE&gt;    1
&lt;/CODE&gt;
    And for more than one dimension:&lt;BR&gt;
&lt;CODE&gt;    my @array1 = (1, 2, 3, 'four');
    my $reference1 = \@array1;
    my @array2 = ('one', 'two', 'three', 4);
    my $reference2 = \@array2;

    my @array = ($reference1, $reference2);

    # this refers to the first item of the first array:
    print $array[0]-&gt;[0];
&lt;/CODE&gt;
    prints:&lt;BR&gt;
&lt;CODE&gt;    1
&lt;/CODE&gt;
&lt;P&gt;
    Okay. &lt;CODE&gt;$array[0]&lt;/CODE&gt; refers to $reference1 and a
reference to an array can use arrow notation to refer
to it's elements.
&lt;/P&gt;
&lt;P&gt;
    &lt;CODE&gt;$reference1-&gt;[0]&lt;/CODE&gt; refers to @array1's first
element. Using a little algebra we can replace like terms.
&lt;CODE&gt;$reference1 = $array[0]&lt;/CODE&gt;, so &lt;CODE&gt;$reference1-&gt;[0]&lt;/CODE&gt;
is the same as &lt;CODE&gt;$array[0]-&gt;[0]&lt;/CODE&gt;.
&lt;/P&gt;
&lt;P&gt;
(Please, take a moment to write a nice email to your under
  appreciated algebra teacher. We'll wait for you.)
&lt;/P&gt;
    You wrote to Gertrude, &lt;EM&gt;didn't you?&lt;/EM&gt; (Shame!)&lt;BR&gt;
&lt;P&gt;
    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 &lt;CODE&gt;[ ]&lt;/CODE&gt;.
&lt;/P&gt;
    Named:&lt;BR&gt;
&lt;CODE&gt;    my @array1 = (1, 2, 3, 'four');
    my $reference1 = \@array1;
&lt;/CODE&gt;
    Anonymous:&lt;BR&gt;
&lt;CODE&gt;    my $reference1 = [1, 2, 3, 'four'];
&lt;/CODE&gt;
    So we could rewrite:&lt;BR&gt;
&lt;CODE&gt;    my @array1 = (1, 2, 3, 'four');
    my $reference1 = \@array1;
    my @array2 = ('one', 'two', 'three', 4);
    my $reference2 = \@array2;

    my @array = ($reference1, $reference2);
    print $array[0]-&gt;[0];
&lt;/CODE&gt;
    As:&lt;BR&gt;
&lt;CODE&gt;    my @array = ( [1, 2, 3, 'four'], ['one', 'two', 'three', 4] );
    print $array[0]-&gt;[0];
&lt;/CODE&gt;
    prints:&lt;BR&gt;
&lt;CODE&gt;    1
&lt;/CODE&gt;
&lt;CENTER&gt; "Trust in the Algebra, Luke" &lt;/CENTER&gt;&lt;BR&gt;
&lt;P&gt;
    Perl allows us to drop the &lt;CODE&gt;-&gt;&lt;/CODE&gt; between subscripts, so we
can also write this as:
&lt;/P&gt;
&lt;CODE&gt;    my @array = ([1, 2, 3, 'four'], ['one', 'two', 'three', 4]);
    print $array[0][0];
&lt;/CODE&gt;
    prints:
&lt;CODE&gt;    1
&lt;/CODE&gt;
&lt;P&gt;
    [perlman:perldsc|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:
&lt;/P&gt;
&lt;CODE&gt;    my @shapes = (
            [qw/circle square triangle polygon/],
            [qw/red green blue yellow fuschia/] );
&lt;/CODE&gt;
&lt;P&gt;
    Data::Dumper provides a the Dumper sub which
will print an array of array references. This is a great debugging
tool:
&lt;/P&gt;
&lt;CODE&gt;
    use Data::Dumper;
    print Dumper \@shapes;
&lt;/CODE&gt;
&lt;P&gt;
    Here's the output one array:
&lt;/P&gt;
&lt;CODE&gt;$VAR1 = [
          [
            'circle',
            'square',
            'triangle',
            'polygon'
          ],
          [
            'red',
            'green',
            'blue',
            'yellow',
            'fuschia'
          ]
        ];
&lt;/CODE&gt;
&lt;P&gt;
    Finally, take a look at [perlman:perllol|Perllol] which describes
Manipulating Arrays of Arrays in Perl and
[perlman:perldata|Perldata], especially the part about slices (and
apple pie).
&lt;/P&gt;
&lt;BR&gt;
&lt;BR&gt;
&lt;BR&gt;
&lt;EM&gt;"The only dumb question is the one you fail to ask."&lt;/EM&gt;&lt;BR&gt;

&lt;p&gt;Update (2 FEB 2009): Fixed a typo and changed array name.&lt;/p&gt;
&lt;p&gt;Update (5 JUN 2012): Added missing code tags around Data::Dumper code.&lt;/p&gt;

</field>
</data>
</node>
