Main Page Content
Multidimensional Arrays In Perl And Javascript
So what does all that mean? Let's go through an
example. Let's simulate a Tic-Tac-Toe board usingan array. We can use a single dimension array,call it board, and map it like this:0 | ! | 1 | ! | 2 |
--- | + | --- | + | --- |
3 | ! | 4 | ! | 5 |
--- | + | --- | + | --- |
6 | ! | 7 | ! | 8 |
So to get the value of the bottom-left corner, in
Perl you'd use$board[6]
while inJavaScript you'd use board[6]
. Usingthis method is a little cumbersome for theprogrammer in that the programmer has to rememberwhich square maps to which array index.A more natural method of labelling the board might
be to use rows and columns. We'll use a pair ofnumbers to indicate each square. The first numberwill be the row that the square is in and thesecond number will the column that it is in.We'll start our numbering of rows and columns fromthe top-left corner. So we have a board thatlooks like this:0,0 | ! | 0,1 | ! | 0,2 |
----- | + | ----- | + | ----- |
1,0 | ! | 1,1 | ! | 1,2 |
----- | + | ----- | + | ----- |
2,0 | ! | 2,1 | ! | 2,2 |
Now to get the value of the bottom-left corner,
we'd want the square in row 2 and column 0, or2,0. The Tic-Tac-Toe board, or any grid, isactually a visualization of a two-dimensionalarray. So ideally, we'd want to create an arraythat could be indexed using the natural method ofrows and columns. In C, you could define thisarray aschar board[3][3];
andaddress the bottom-left corner asboard[2][0]
but in Perl andJavaScript you can't do this as the languagedoesn't support it. However, you can emulate thisbehaviour in both languages in basically the sameway.Both Perl and JavaScript allow you to create
arrays that contain references to other arrays.So to emulate the above structure, we can use aone-dimensional array (the rows) that onlycontains references to other one-dimensionalarrays (the columns). In Perl this would looklike: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 pointto a column array. So if we wanted thebottom-left corner, we'd want$rows[2]->[0]
which means:- Look into the rows array at position 3 (everything starts from 0).
- Since the rows array only contains a 'pointer'to another array, we use the -> syntax to meanlook into the array that is being pointed to.
- 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. A similar thing can be done inJavaScript. To set up the arrays we'd use:var rows = new Array();
var col0 = new Array();
var col1 = new Array();
var col2 = new Array();
rows[0] = col0;
rows[1] = col1;
rows[2] = col2;
And to get the bottom-left corner we'd use
rows[2][0]
. Perl will also supportthis syntax as a short hand for the pointer methodabove; you'd write $rows[2][0]
.The above method to declare your arrays is a
little cumbersome and both Perl and JavaScript letyou combine the statements into a singlestatement. For Perl you can use:my @board = ( [], [], [] );
which means create an array with references to
three other arrays inside. While in JavaScriptyou can use:var board = new Array ( new Array(), new Array(), new Array() );
It should be noted that you do not need
to declare these arrays in Perl or JavaScript.You can simply start adding values to the arrays.So to make the declaration more useful, we can addinitialization of the inside arrays at the sametime. Say you wanted to set the value of eachcell to the number of its one-dimensionalcounterpart. In Perl you would use:my @board = ( | ['0', '1', '2'], |
['3', '4', '5'], | |
['6', '7', '8'] ); |
while in JavaScript you would use:
var board = new Array( | new Array("0", "1", "2"), |
new Array("3", "4", "5"), | |
new Array("6", "7", "8") ); |
Now that we've conquered two-dimensional arrays,
we can extend our idea to the three-dimensionalcase. Let's take a look at the Perlinitialization of a three-dimensional array, thinkof 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 upof three arrays which represent the rows of thelevel, which in turn are composed of three morearrays which represent the columns of the level.So each level of the board is made up of aTic-Tac-Toe board of its own so we have 27 squaresin total.We can expand this 'array-of-arrays' notion to
four dimensions and beyond. But as you can see inthe three-dimensional case, it's already beginningto get a little confusing. As well, visualizingthese structures beyond the fourth dimension canbe a little tricky.So that's multidimensional arrays. If you have
any questions or comments, feel free to e-mail meat dmah@vox.org.Note: Perl purists will note that
I use the term 'array' in place of 'list' to avoidconfusion with terminology. If you can tell thedifference between the two, you probably don'tneed to read this article anyway!