Main Page Content
Multidimensional Arrays In Perl and JavaScript
Rated 3.56 (Ratings: 3) (Add your rating)
Log in to add a comment
(2 comments so far)
Unlike C and Pascal, there is no language support
for multidimensional arrays in Perl or JavaScript.
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.
So what does all that mean? Let's go through an
example. Let's simulate a Tic-Tac-Toe board using
an array. We can use a single dimension array,
call it board, and map it like this:
So to get the value of the bottom-left corner, in
Perl you'd use
Now to get the value of the bottom-left corner,
we'd want the square in row 2 and column 0, or
2,0. The Tic-Tac-Toe board, or any grid, is
actually a visualization of a two-dimensional
array. So ideally, we'd want to create an array
that could be indexed using the natural method of
rows and columns. In C, you could define this
array as
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
And to get the bottom-left corner we'd use
which means create an array with references to
three other arrays inside. While in JavaScript
you can use:
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 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:
while in JavaScript you would use:
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.
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.
| 0 | ! | 1 | ! | 2 |
| --- | + | --- | + | --- |
| 3 | ! | 4 | ! | 5 |
| --- | + | --- | + | --- |
| 6 | ! | 7 | ! | 8 |
$board[6] while in
JavaScript you'd use board[6]. Using
this method is a little cumbersome for the
programmer in that the programmer has to remember
which 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 of
numbers to indicate each square. The first number
will be the row that the square is in and the
second number will the column that it is in.
We'll start our numbering of rows and columns from
the top-left corner. So we have a board that
looks like this:
| 0,0 | ! | 0,1 | ! | 0,2 |
| ----- | + | ----- | + | ----- |
| 1,0 | ! | 1,1 | ! | 1,2 |
| ----- | + | ----- | + | ----- |
| 2,0 | ! | 2,1 | ! | 2,2 |
char board[3][3]; and
address the bottom-left corner as
board[2][0] but in Perl and
JavaScript you can't do this as the language
doesn't support it. However, you can emulate this
behaviour in both languages in basically the same
way.
Both Perl and JavaScript allow you to create
arrays that contain references to other arrays.
So to emulate the above structure, 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;<br>
my @col0;<br>
my @col1;<br>
my @col2;<br>
$rows[0] = \@col0;<br>
$rows[1] = \@col1;<br>
$rows[2] = \@col2;$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 mean look into the array that is being pointed to.
- And then finally, return the value in position 1 of the array.
var rows = new Array();<br>
var col0 = new Array();<br>
var col1 = new Array();<br>
var col2 = new Array();<br>
rows[0] = col0;<br>
rows[1] = col1;<br>
rows[2] = col2;rows[2][0]. Perl will also support
this syntax as a short hand for the pointer method
above; you'd write $rows[2][0].
The above method to declare your arrays is a
little cumbersome and both Perl and JavaScript let
you combine the statements into a single
statement. For Perl you can use:
my @board = ( [], [], [] );
var board = new Array ( new Array(), new Array(), new Array() );
| my @board = ( | ['0', '1', '2'], |
| ['3', '4', '5'], | |
| ['6', '7', '8'] ); |
| var board = new Array( | new Array("0", "1", "2"), |
| new Array("3", "4", "5"), | |
| new Array("6", "7", "8") ); |
| 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'] | ||
| ] | ||
| ); |
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!




