Perl Array of Array: Using Multidimensional Arrays in Perl

In Perl you can define, initialize and use an ordinary, one-dimensional array like this:

my @fruits = ('apple', 'orange', 'pear');

# Display the element at index 1
# (second element in the array)
print $fruits[1];


Defining and using multidimensional arrays is not much harder; but before we look at how to do it, you need to understand array references in Perl.

References to Arrays in Perl

Take a look at the following code. If you can understand the following, you basically know all you need to know about array references in Perl.

References are scalar values that "point" to some more complex data type. They are particularly useful for passing to functions or returning from functions; -- and as you'll see, we can also use them to form multidimensional arrays in Perl.

# This is an array.
my @fruits = ('apple', 'orange', 'pear');

# This is a reference to an array.
my $creepy = ['snake', 'spider', 'ant'];

# Access an array element
print $fruits[1], "\n";

# Access an array element via a reference.
print $creepy->[2], "\n";

# Get a reference to @fruits.
my $fruits = @fruits;

# Access fruits array via its reference.
print $fruits->[0], "\n";


You can see that working with array references in Perl is very similar to working directly with arrays. We need to use an extra -> operator to access elements, and we use square brackets to initialize array references instead of round brackets for arrays, but there's not a huge difference other than that.

Arrays of Arrays in Perl -- An Example

Since references to arrays are scalar values, we can use them to form multidimensional arrays. The follow example initializes and uses a multidimensional array in Perl; -- actually simply an array of references.

my @stuff = (
    ['one', 'two', 'three', 'four'],
    [7, 6, 5],
    ['apple', 'orange'],
    [0.3, 'random', 'stuff', 'here', 5],

print $stuff[0][2], "\n";

print $stuff[3][1], "\n";


You can see that our "array of arrays" is actually an array of references to arrays. Since we have an array of references (scalar values), each reference can point to an array of any length; there's no requirement for all arrays in the parent array to be the same length.

As long as you keep in mind that you're actually dealing with an array of references, it's easy to remember how to access the elements. In the above example, $stuff[0] refers to the first array reference ['one', 'two', 'three' ...], $stuff[1] refers to the second array reference [7, 6, 5] and so on.

After adding the subscript to get you to the right reference, just add another index to go to the right element in that sub-array.

$stuff[1][2] is the element at position 2 in the array at position 1, for instance.

Arrays of Hashes in Perl

Finally, it's worth noting here that we can form arrays of other data structures just as easily using the same method.

Here's an example of an array of hashes (that is, an array of references to hashes). You could also have an array containing assorted references of all kinds, if you went particularly heavy on the LSD last night. But if you do that, I hope I never have to read your code ...

my @stuff = (
        'Jack' => 'stamp collecting', 
        'Pete' => 'golf',
        'Lisa' => 'bus surfing',
        1 => 'Mon',
        2 => 'Tues',
        3 => 'Wed',
        # etc.

print $stuff[0]{'Jack'}, "\n";
print $stuff[1]{2}, "\n";

stamp collecting

For more information on arrays in Perl, check out the Cave of Programming Guide to Arrays in Perl