Perl Three-Dimensional Array
Perl Multi-Dimensional Array - Part 3
Forward: In this part of the series, I talk about the basics of Perl three-dimensional array; that is, the basics of Perl 3D array.
By: Chrysanthus Date Published: 2 Dec 2012
Introduction
Illustration Example
Consider a large cubic box made up of small regular cubic cells. Assume that a surface of the large cubic box is aligned with the computer screen and the box extends into the screen. Assume that the first wall of cells is what you see at the computer screen. In this case, the first data table below is for the first wall. The second data table is for the second wall, going into the computer screen. The third wall is behind this, going further into the computer screen. Then you have the fourth and fifth vertical walls, further and further behind, into the computer screen. The 5 different tables below have data corresponding to these 5 walls; each datum per cell. The cubic box consists of walls with no space between the walls. A cell is like a block in real life walls.
The three axes are represented by i, j and k. i is for the rows (left-to-right), j is for the columns (top-down), and k is for the inward direction into the computer screen (or book). The box is a 5 X 5 X 5 container. For simplicity, the value (content) of each cell is the co-ordinate of the cell, in string form. So, for the i, j, k order, and with indexing beginning from zero, the values of the cells are:
"0,0,0", "0,1,0", "0,2,0", "0,3,0", "0,4,0"
"1,0,0", "1,1,0", "1,2,0", "1,3,0", "1,4,0"
"2,0,0", "2,1,0", "2,2,0", "2,3,0", "2,4,0"
"3,0,0", "3,1,0", "3,2,0", "3,3,0", "3,4,0"
"4,0,0", "4,1,0", "4,2,0", "4,3,0", "4,4,0"
"0,0,1", "0,1,1", "0,2,1", "0,3,1", "0,4,1"
"1,0,1", "1,1,1", "1,2,1", "1,3,1", "1,4,1"
"2,0,1", "2,1,1", "2,2,1", "2,3,1", "2,4,1"
"3,0,1", "3,1,1", "3,2,1", "3,3,1", "3,4,1"
"4,0,1", "4,1,1", "4,2,1", "4,3,1", "4,4,1"
"0,0,2", "0,1,2", "0,2,2", "0,3,2", "0,4,2"
"1,0,2", "1,1,2", "1,2,2", "1,3,2", "1,4,2"
"2,0,2", "2,1,2", "2,2,2", "2,3,2", "2,4,2"
"3,0,2", "3,1,2", "3,2,2", "3,3,2", "3,4,2"
"4,0,2", "4,1,2", "4,2,2", "4,3,2", "4,4,2"
"0,0,3", "0,1,3", "0,2,3", "0,3,3", "0,4,3"
"1,0,3", "1,1,3", "1,2,3", "1,3,3", "1,4,3"
"2,0,3", "2,1,3", "2,2,3", "2,3,3", "2,4,3"
"3,0,3", "3,1,3", "3,2,3", "3,3,3", "3,4,3"
"4,0,3", "4,1,3", "4,2,3", "4,3,3", "4,4,3"
"0,0,4", "0,1,4", "0,2,4", "0,3,4", "0,4,4"
"1,0,4", "1,1,4", "1,2,4", "1,3,4", "1,4,4"
"2,0,4", "2,1,4", "2,2,4", "2,3,4", "2,4,4"
"3,0,4", "3,1,4", "3,2,4", "3,3,4", "3,4,4"
"4,0,4", "4,1,4", "4,2,4", "4,3,4", "4,4,4"
A three-dimensional array has rows, columns and vertical walls. The walls are stacked next to one another going into the screen. Each wall has rows and columns, of cells. Counting of rows begins from zero. Counting of columns also begins from zero. Counting of walls also begins from zero. In Perl Three-Dimensional Array, you quote the row position (index) first before you quote the column position, then you quote the wall position. So the value of [0][0][0] in the above tables is, "0,0,0". This means the row concerned is row index zero, the column concerned is column zero, and the wall concerned is wall index zero (actually wall position 1). The value of [0][1][0] in the above tables is, "0,1,0" in wall zero (actually wall position 1). The value of [1][0][1] in the above tables is, "1,0,1" in wall 1 (actually wall position 2). The value of [2][2][2] is, "2,2,2" in wall 2 (actually wall position 3); and so on. The value, "2,2,2" is in the middle of the above cube.
Just as the wall of a house consists of blocks, the vertical walls of a 3D array consists cells; you can call the cells, elements. So, consider a 3D array as a cuboid consisting of solid walls without space between the walls.
Creating a Three-Dimensional Array
You create an empty three-dimensional array in the same way that you create an empty one-dimensional array. Let the name of our three-dimensional array to be created be, arr. So you would create the empty three-dimensional array as follows:
my @arr;
Accessing an Array Element
You access an element (cell) in a three-dimensional array with the following syntax:
$arrayName[rowIndex][columnIndex][wallIndex]
You will see examples below.
Placing Elements into a 3D Array One-by-One
You can place Elements into a three-dimensional array one-by-one. You do this using the square brackets and the corresponding indices (row, column and wall numbers). The following code places the first (index 0) and second (index 1) walls of the above tables into an array, one element at a time:
use strict;
my @arr;
$arr[0][0][0]="0,0,0";$arr[0][1][0]="0,1,0";$arr[0][2][0]="0,2,0";$arr[0][3][0]="0,3,0";$arr[0][4][0]="0,4,0";
$arr[1][0][0]="1,0,0";$arr[1][1][0]="1,1,0";$arr[1][2][0]="0,2,0";$arr[1][3][0]="1,3,0";$arr[1][4][0]="1,4,0";
$arr[2][0][0]="2,0,0";$arr[2][1][0]="2,1,0";$arr[2][2][0]="2,2,0";$arr[2][3][0]="2,3,0";$arr[2][4][0]="2,4,0";
$arr[3][0][0]="3,0,0";$arr[3][1][0]="3,1,0";$arr[3][2][0]="4,2,0";$arr[3][3][0]="3,3,0";$arr[3][4][0]="3,4,0";
$arr[4][0][0]="4,0,0";$arr[4][1][0]="4,1,0";$arr[4][2][0]="4,2,0";$arr[4][3][0]="4,3,0";$arr[4][4][0]="4,4,0";
$arr[0][0][1]="0,0,1";$arr[0][1][1]="0,1,1";$arr[0][2][1]="0,2,1";$arr[0][3][1]="0,3,1";$arr[0][4][1]="0,4,1";
$arr[1][0][1]="1,0,1";$arr[1][1][1]="1,1,1";$arr[1][2][1]="0,2,1";$arr[1][3][1]="1,3,1";$arr[1][4][1]="1,4,1";
$arr[2][0][1]="2,0,1";$arr[2][1][1]="2,1,1";$arr[2][2][1]="2,2,1";$arr[2][3][1]="2,3,1";$arr[2][4][1]="2,4,1";
$arr[3][0][1]="3,0,1";$arr[3][1][1]="3,1,1";$arr[3][2][1]="4,2,1";$arr[3][3][1]="3,3,1";$arr[3][4][1]="3,4,1";
$arr[4][0][1]="4,0,1";$arr[4][1][1]="4,1,1";$arr[4][2][1]="4,2,1";$arr[4][3][1]="4,3,1";$arr[4][4][1]="4,4,1";
Note: You can replace a value in a cell in the same way that you assign value for the first time. Also note the quotes used with strings above. If a value is a number, it is not typed in quotes.
The syntax to read a value from a three-dimensional array into a variable is:
$var = $arrayName[rowIndex][columnIndex][wallIndex];
So you read a value in a similar way that you place a value. The following code would place the first two walls of the above tables into an array and then prints the values:
use strict;
my @arr;
$arr[0][0][0]="0,0,0";$arr[0][1][0]="0,1,0";$arr[0][2][0]="0,2,0";$arr[0][3][0]="0,3,0";$arr[0][4][0]="0,4,0";
$arr[1][0][0]="1,0,0";$arr[1][1][0]="1,1,0";$arr[1][2][0]="0,2,0";$arr[1][3][0]="1,3,0";$arr[1][4][0]="1,4,0";
$arr[2][0][0]="2,0,0";$arr[2][1][0]="2,1,0";$arr[2][2][0]="2,2,0";$arr[2][3][0]="2,3,0";$arr[2][4][0]="2,4,0";
$arr[3][0][0]="3,0,0";$arr[3][1][0]="3,1,0";$arr[3][2][0]="4,2,0";$arr[3][3][0]="3,3,0";$arr[3][4][0]="3,4,0";
$arr[4][0][0]="4,0,0";$arr[4][1][0]="4,1,0";$arr[4][2][0]="4,2,0";$arr[4][3][0]="4,3,0";$arr[4][4][0]="4,4,0";
$arr[0][0][1]="0,0,1";$arr[0][1][1]="0,1,1";$arr[0][2][1]="0,2,1";$arr[0][3][1]="0,3,1";$arr[0][4][1]="0,4,1";
$arr[1][0][1]="1,0,1";$arr[1][1][1]="1,1,1";$arr[1][2][1]="0,2,1";$arr[1][3][1]="1,3,1";$arr[1][4][1]="1,4,1";
$arr[2][0][1]="2,0,1";$arr[2][1][1]="2,1,1";$arr[2][2][1]="2,2,1";$arr[2][3][1]="2,3,1";$arr[2][4][1]="2,4,1";
$arr[3][0][1]="3,0,1";$arr[3][1][1]="3,1,1";$arr[3][2][1]="4,2,1";$arr[3][3][1]="3,3,1";$arr[3][4][1]="3,4,1";
$arr[4][0][1]="4,0,1";$arr[4][1][1]="4,1,1";$arr[4][2][1]="4,2,1";$arr[4][3][1]="4,3,1";$arr[4][4][1]="4,4,1";
for (my $k=0; $k<2; ++$k)
{
for (my $i=0; $i<5; ++$i)
{
for (my $j=0; $j<5; ++$j)
{
print $arr[$i][$j][$k] . ", ";
}
print "\n";
}
print "\n";
}
In the for-loop, $i is for the row index, $j is for the column index and $k is for the wall index (that is, i for rows, j for columns and k for walls). In the output, the quotes have not been indicated. Read and try the code. In a commercial program you would have to modify the code to remove the last comma displayed at the end of a row.
Above, I have looked at a 3D array in terms of vertical walls. You can also look at a 3D array in terms of horizontal flat layers. A 3D array can be considered to be made up of horizontal rectangular layers, stacked one below the other. The first layer is at the top; the second below; the third below the second; and so on.
Each layer consists of element rows moving from front to back (into the screen). Above, in the case of walls, rows are horizontal moving from left to right. In this model, the rows are moving from front to back, with the first, in a layer, on the left (going into the screen).
With this model, the values (elements) of the first two horizontal layers of the above tables of data, are:
"0,0,0", "0,0,1", "0,0,2", "0,0,3", "0,0,4"
"0,1,0", "0,1,1", "0,1,2", "0,1,3", "0,1,4"
"0,2,0", "0,2,1", "0,2,2", "0,2,3", "0,2,4"
"0,3,0", "0,3,1", "0,3,2", "0,3,3", "0,3,4"
"0,4,0", "0,4,1", "0,4,2", "0,4,3", "0,4,4"
"1,0,0", "1,0,1", "1,0,2", "1,0,3", "1,0,4"
"1,1,0", "1,1,1", "1,1,2", "1,1,3", "1,1,4"
"1,2,0", "1,2,1", "1,2,2", "1,2,3", "1,2,4"
"1,3,0", "1,3,1", "1,3,2", "1,3,3", "1,3,4"
"1,4,0", "1,4,1", "1,4,2", "1,4,3", "1,4,4"
A layer is a 2D horizontal rectangular array, not a wall. The programming code for the first layer is:
[
["0,0,0", "0,0,1", "0,0,2", "0,0,3", "0,0,4"],
["0,1,0", "0,1,1", "0,1,2", "0,1,3", "0,1,4"],
["0,2,0", "0,2,1", "0,2,2", "0,2,3", "0,2,4"],
["0,3,0", "0,3,1", "0,3,2", "0,3,3", "0,3,4"],
["0,4,0", "0,4,1", "0,4,2", "0,4,3", "0,4,4"]
]
Note the use of the square bracket pairs, especially the top-most bracket and the bottom-most bracket. A single array, whether moving from left to right or front to back is enclosed in a pair of square brackets. All layers are coded in the same way.
The following program copies the above two layers into an empty array and displays the values in wall format of 5 walls; each wall of height, 2:
use strict;
my @arr;
@arr[0] = [
["0,0,0", "0,0,1", "0,0,2", "0,0,3", "0,0,4"],
["0,1,0", "0,1,1", "0,1,2", "0,1,3", "0,1,4"],
["0,2,0", "0,2,1", "0,2,2", "0,2,3", "0,2,4"],
["0,3,0", "0,3,1", "0,3,2", "0,3,3", "0,3,4"],
["0,4,0", "0,4,1", "0,4,2", "0,4,3", "0,4,4"]
];
@arr[1] = [
["1,0,0", "1,0,1", "1,0,2", "1,0,3", "1,0,4"],
["1,1,0", "1,1,1", "1,1,2", "1,1,3", "1,1,4"],
["1,2,0", "1,2,1", "1,2,2", "1,2,3", "1,2,4"],
["1,3,0", "1,3,1", "1,3,2", "1,3,3", "1,3,4"],
["1,4,0", "1,4,1", "1,4,2", "1,4,3", "1,4,4"]
];
for (my $k=0; $k<5; ++$k)
{
for (my $i=0; $i<2; ++$i)
{
for (my $j=0; $j<5; ++$j)
{
print $arr[$i][$j][$k] . ", ";
}
print "\n";
}
print "\n";
}
Placing elements layer-by-layer for a 3D array is similar to placing elements row-by-row for a 2D array. For both cases you use the array name preceded by @. Note: the variable, $i is for the different horizontal rows in a wall, the variable $j is for the different vertical columns in a wall, and the variable $k is for vertical walls from front to back; always. Do not confuse between the vertical wall model and the horizontal layer model, in Perl 3D Array.
To create 1D array by initialization, you place all the values within a pair of square brackets. To create a 2D array by initialization, you place a set of the 1D arrays within a pair of square brackets. To create a 3D array by initialization, you place a set of 2D arrays within a pair of square brackets. You assign the assembly to a scalar variable. This scalar variable actually holds a reference, but do not worry about that for now. To access the elements of the 3D array, you type the name of the array, preceded by two $ symbols, followed by three square bracket pairs, each having the relevant index. Read and try the following program that illustrates this for the first two walls of the above tables, in layer format. Note: a row of values (elements) here, moves into the screen and not from left to right as with the 2D array. So, here, you have two walls with the first part of 5 layers.
use strict;
my $threeDArrRef;
$threeDArrRef = [
[
["0,0,0", "0,0,1"],
["0,1,0", "0,1,1"],
["0,2,0", "0,2,1"],
["0,3,0", "0,3,1"],
["0,4,0", "0,4,1"]
],
[
["1,0,0", "1,0,1"],
["1,1,0", "1,1,1"],
["1,2,0", "1,2,1"],
["1,3,0", "1,3,1"],
["1,4,0", "1,4,1"]
],
[
["2,0,0", "2,0,1"],
["2,1,0", "2,1,1"],
["2,2,0", "2,2,1"],
["2,3,0", "2,3,1"],
["2,4,0", "2,4,1"]
],
[
["3,0,0", "3,0,1"],
["3,1,0", "3,1,1"],
["3,2,0", "3,2,1"],
["3,3,0", "3,3,1"],
["3,4,0", "3,4,1"]
],
[
["4,0,0", "4,0,1"],
["4,1,0", "4,1,1"],
["4,2,0", "4,2,1"],
["4,3,0", "4,3,1"],
["4,4,0", "4,4,1"]
]
];
for (my $k=0; $k<2; ++$k)
{
for (my $i=0; $i<5; ++$i)
{
for (my $j=0; $j<5; ++$j)
{
print $$threeDArrRef[$i][$j][$k] . ", ";
}
print "\n";
}
print "\n";
}
Note: A Perl 3D initialization in code, is not typed wall-by-wall; it is typed layer-by-layer where each row goes into the screen (from front to behind).
The 1D arrays are separated by commas; the 2D arrays are separated by commas. There is no comma after the last item or array within a set. The quotes have not been taken into account in the output.
End of Tutorial and End of Series
We have come to the end of the tutorial and end of the series. I hope you appreciated the series.
Chrys
Related Links
Perl BasicsPerl Data Types
Perl Syntax
Perl References Optimized
Handling Files and Directories in Perl
Perl Function
Perl Package
Perl Object Oriented Programming
Perl Regular Expressions
Perl Operators
Perl Core Number Basics and Testing
Commonly Used Perl Predefined Functions
Line Oriented Operator and Here-doc
Handling Strings in Perl
Using Perl Arrays
Using Perl Hashes
Perl Multi-Dimensional Array
Date and Time in Perl
Perl Scoping
Namespace in Perl
Perl Eval Function
Writing a Perl Command Line Tool
Perl Insecurities and Prevention
Sending Email with Perl
Advanced Course
Miscellaneous Features in Perl
Perl Two-Dimensional Structures
Advanced Perl Regular Expressions
Designing and Using a Perl Module
More Related Links
Perl Mailsend
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library
MySQL Course
BACK