If you used Data::Dump (or Data::Dumper) to display your datastructure, the reason becomes clear:

#! perl -slw use strict; use Data::Dump qw[ pp ]; 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; pp \@sheet; __END__ c:\test>junk38 do { my $a = [ [[0, 0, 0], [0, 0, 5], [0, 0, 0]], ['fix', 'fix', 'fix'], ['fix', 'fix', 'fix'], ]; $a->[1][0] = $a->[0][0]; $a->[1][1] = $a->[0][1]; $a->[1][2] = $a->[0][2]; $a->[2][0] = $a->[0][0]; $a->[2][1] = $a->[0][1]; $a->[2][2] = $a->[0][2]; $a; }

The reason is this line:

for($sheet = 0; $sheet < 3; $sheet++){push @sheet, [@row];}

The [@row] does a deep copy of the contents of @row, but a shallow copy of the things contained within those contents. Hence, the same arrays appear as as subarrays of the higher levels. (That's not a good description, but the best I could come up with.)

Perhaps this corrected code will explain things better:

#! perl -slw use strict; use Data::Dump qw[ pp ]; 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, [ map [ @$_ ], @row ]; } $sheet[0][1][2] = 5; pp \@sheet; __END__ c:\test>junk38 [ [[0, 0, 0], [0, 0, 5], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0]], ]

And for reference, here's how I would initialise that construct:

#! perl -slw use strict; use Data::Dump qw[ pp ]; my @sheets = map { [ map { [ ( 0 ) x 3 ] } 1 .. 3 ] } 1 .. 3; $sheets[0][1][2] = 5; pp \@sheets; __END__ c:\test>junk38 [ [[0, 0, 0], [0, 0, 5], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0]], ]

Which I find more captures the operation and therefore easier to follow, but others might not agree.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re: Creating and Accessing 3-D array by BrowserUk
in thread 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.