Skip to page content or Skip to Accesskey List.


Main Page Content

Multidimensional Arrays In Perl And Javascript

Rated 3.56 (Ratings: 3)

Want more?

Picture of dmah

Dean Mah

Member info

User since: 05 Jun 1999

Articles written: 25

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:

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 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

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 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


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


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. A similar thing can be done in

JavaScript. 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 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 = ( [], [], [] );

which means create an array with references to

three other arrays inside. While in JavaScript

you 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 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'] );

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-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


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!

Founding member of
Dean Mah

The access keys for this page are: ALT (Control on a Mac) plus: is an all-volunteer resource for web developers made up of a discussion list, a browser archive, and member-submitted articles. This article is the property of its author, please do not redistribute or use elsewhere without checking with the author.