Posted on 05 Aug 1999
by Dean Mah (dmah)
Rated 3.56 (Ratings: 3)
- More articles in Backend
So what does all that mean? Let's go through anexample. 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:
So to get the value of the bottom-left corner, inPerl you'd use
board. 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 mightbe 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:
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 as
char board;andaddress the bottom-left corner as
$rows = \@col0;
$rows = \@col1;
$rows = \@col2;
We create the first array that represents the rowsand 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
- 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.
var rows = new Array();
var col0 = new Array();
var col1 = new Array();
var col2 = new Array();
rows = col0;
rows = col1;
rows = col2;
And to get the bottom-left corner we'd use
rows. Perl will also supportthis syntax as a short hand for the pointer methodabove; you'd write
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") );|
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 tofour 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 haveany questions or comments, feel free to e-mail meat email@example.com.
Note: Perl purists will note thatI 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!