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..)In reply to Creating and Accessing 3-D array by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |