in reply to Creating and Accessing 3-D array

How many row arrays do you create in total? 3. How many rows do you want? 9, 3 per sheet. Do you see the problem?

Another hint would be that your loop structure for populating the array is different than your loop structure for getting from the array, yet you are looping over the same array. How can that be?

Fix:

for my $sheet (1..3) { my @sheet; for my $row (1..3) { my @row; for my $col (1..3) { push @col, 0; } push @sheet, \@row; } push @sheets, \@sheet; }

Thanks to autovivification, this can actually be simplified.

for my $sheet (1..3) { for my $row (1..3) { for my $col (1..3) { $sheets[$sheet][$row][$col] = 0; } } }

Replies are listed 'Best First'.
Re^2: Creating and Accessing 3-D array
by Anonymous Monk on Feb 06, 2011 at 13:51 UTC

    Yes, I figured out my mistake.

    Autovivification is great tool. I did it that way (lazy me :-))

    In my opinion, autovivification should not be taught to a beginner. It makes creating multidimentional arrays so simple that, lazy people like me will never study data structures chapter .. :-)

    Thanks

    Chak

    ---

    Laziness is the mother of invention (My philosophy)

      The equivalent without autovivification of the snippet using autovivification is

      ( ( $sheets[$sheet] //= [] )->[$row] //= [] )->[$col] = 0;

      Your choice.