Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Hi,
I have a query regarding creating and accessing elements of a 3-D Array. Below is a code for 3D array. It has three sheets of three columns and three rows each. It is made by making a column array of 3 elements. This column array is pushed inside 3 row arrays. And these 3 row arrays are pushed inside a sheet arraymy $col; my @col; my $row; my @row; my $sheet; my @sheet; for($col = 0; $col < 3; $col++) { push @col, "0";} for($row = 0; $row < 3; $row++) { push @row, [@col];} for($sheet = 0; $sheet < 3; $sheet++){push @sheet, [@row];} $sheet[0][1][2] = 5; for($col = 0; $col < 3; $col++) { for($row = 0; $row < 3; $row++) { for($sheet = 0; $sheet < 3; $sheet++){ print "sheet=$sheet,row=$row,col=$col,$sheet[$sheet][$row][$col]\n"; } } }
Expected output is - all elements containing "0"'s inside them, except for $sheet[0][1][2] element, which has "5". But My output is showing three elements, $sheet[0][1][2], $sheet[1][1][2], $sheet[2][1][2] having 5 in them.
Below is full outputsheet = 0, row = 0, col = 0, 0 sheet = 1, row = 0, col = 0, 0 sheet = 2, row = 0, col = 0, 0 sheet = 0, row = 1, col = 0, 0 sheet = 1, row = 1, col = 0, 0 sheet = 2, row = 1, col = 0, 0 sheet = 0, row = 2, col = 0, 0 sheet = 1, row = 2, col = 0, 0 sheet = 2, row = 2, col = 0, 0 sheet = 0, row = 0, col = 1, 0 sheet = 1, row = 0, col = 1, 0 sheet = 2, row = 0, col = 1, 0 sheet = 0, row = 1, col = 1, 0 sheet = 1, row = 1, col = 1, 0 sheet = 2, row = 1, col = 1, 0 sheet = 0, row = 2, col = 1, 0 sheet = 1, row = 2, col = 1, 0 sheet = 2, row = 2, col = 1, 0 sheet = 0, row = 0, col = 2, 0 sheet = 1, row = 0, col = 2, 0 sheet = 2, row = 0, col = 2, 0 sheet = 0, row = 1, col = 2, 5 sheet = 1, row = 1, col = 2, 5 sheet = 2, row = 1, col = 2, 5 sheet = 0, row = 2, col = 2, 0 sheet = 1, row = 2, col = 2, 0 sheet = 2, row = 2, col = 2, 0
My understanding of making 3-D arrays is not clear. Can any one explain the concept of 3-D arrays, from this example ?
Thanks Chak (forgot my login details..)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Creating and Accessing 3-D array
by BrowserUk (Patriarch) on Feb 06, 2011 at 11:26 UTC | |
by Anonymous Monk on Feb 06, 2011 at 13:43 UTC | |
by Anonymous Monk on Feb 07, 2011 at 05:27 UTC | |
|
Re: Creating and Accessing 3-D array
by ikegami (Patriarch) on Feb 06, 2011 at 11:29 UTC | |
by Anonymous Monk on Feb 06, 2011 at 13:51 UTC | |
by ikegami (Patriarch) on Feb 07, 2011 at 02:26 UTC | |
|
Re: Creating and Accessing 3-D array
by locked_user sundialsvc4 (Abbot) on Feb 07, 2011 at 17:57 UTC |