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 array

my $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 output

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.