Multidimensional Arrays in Perl

This article was originally hosted here and written by Dean Mah for Evolt.org. It dealt with both JS and Perl. I took out the JS part. If you want JS, go to the link.
Unlike C and Pascal, there is no language support for multidimensional arrays in Perl. While it is true that single dimension arrays are sufficient for all tasks (memory is conceptually a single dimension array), multidimensional arrays provide a notational convenience so the programmer does not have to worry about the array indexing scheme and overhead. Perl allows you to create arrays that contain references to other arrays. So to emulate a multidimensional array, we can use a one-dimensional array (the rows) that only contains references to other one-dimensional arrays (the columns). In Perl this would look like:

my @rows;
my @col0;
my @col1;
my @col2;
$rows[0] = \@col0;
$rows[1] = \@col1;
$rows[2] = \@col2;

We create the first array that represents the rows and three other arrays that represent each column. We then set each cell of the rows array to point to a column array. So if we wanted the bottom-left corner, we'd want $rows[2]->[0] which means:
  1. Look into the rows array at position 3 (everything starts from 0).
  2. Since the rows array only contains a 'pointer' to another array, we use the -> syntax to mean look into the array that is being pointed to.
  3. And then finally, return the value in position 1 of the array.
So what we get returned is the value in row 2, column 0. Perl will also support a short hand syntax for the pointer method above; you'd write $rows[2][0]. The above method to declare your arrays is a little cumbersome and so Perl lets you combine the statements into a single statement. For Perl you can use:

my @board = ( [], [], [] );

which means create an array with references to three other arrays inside. It should be noted that you do not need to declare these arrays in Perl. You can simply start adding values to the arrays. So to make the declaration more useful, we can add initialization of the inside arrays at the same time. Say you wanted to set the value of each cell to the number of its one-dimensional counterpart. In Perl you would use:

my @board = ( ['0', '1', '2'],
 ['3', '4', '5'],
 ['6', '7', '8'] );

Now that we've conquered two-dimensional arrays, we can extend our idea to the three-dimensional case. Let's take a look at the Perl initialization of a three-dimensional array, think of a 3-D Tic-Tac-Toe board.

my @board = ( [ 
  ['0', '1', '2'],
  ['3', '4', '5'],
  ['6', '7', '8']
 ], 
 [ 
  ['9', '10', '11'],
  ['12', '13', '14'],
  ['15', '16', '17']
 ], 
 [ 
  ['18', '19', '20'],
  ['21', '22', '23'],
  ['24', '25', '26']
 ] 
);  

So we have the outer array which represents the 'level,' starting from the top, which are made up of three arrays which represent the rows of the level, which in turn are composed of three more arrays which represent the columns of the level. So each level of the board is made up of a Tic-Tac-Toe board of its own so we have 27 squares in total. We can expand this 'array-of-arrays' notion to four dimensions and beyond. But as you can see in the three-dimensional case, it's already beginning to get a little confusing. As well, visualizing these structures beyond the fourth dimension can be a little tricky. So that's multidimensional arrays. If you have any questions or comments, feel free to e-mail me at dmah@vox.org.

Note: Perl purists will note that I use the term 'array' in place of 'list' to avoid confusion with terminology. If you can tell the difference between the two, you probably don't need to read this article anyway!


[Home Page] [Portfolio Website] [Writings] [Family] [Photo Album] [Reading Material] [Resources] [My Blogs] [Mei Sententia] [Sites of Interest] [Search Site]

Last updated: 2348 hrs PST, Thursday, March 08, 2007