burgerw has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
I've playing around with a multidimensional array and found something that confusses me royally.
What is so special about $a_final[1][1][0][0] in the following snippet?
I can assign all the other elements (or at least, all I've tested). But not this one.
Can you help me?
Thank you very much.
burgerw

@a_1 = (); @a_2 = (); @a_3 = (); @a_4 = (); @a_a = (\a_4, \a_3); @a_b = (\@a_2, \a_a); @a_final = (\@a_1, \@a_b); #These do work: $a_final[0][1][0][0] = "x"; print "$a_final[0][1][0][0]\n"; $a_final[1][0][0][0] = "x"; print "$a_final[1][0][0][0]\n"; $a_final[1][2][0][0] = "x"; print "$a_final[1][2][0][0]\n"; # This won't work: # error message: "Not an ARRAY reference" $a_final[1][1][0][0] = "x"; print "$a_final[1][1][0][0]\n";

Replies are listed 'Best First'.
Re: Multidimensional Array
by everybody (Scribe) on Apr 15, 2010 at 13:08 UTC
    The reason it's saying "Not an ARRAY reference" is because it's not an array reference. Add the missing sigils, and the error disappears:
    ... @a_a = (\@a_4, \@a_3); @a_b = (\@a_2, \@a_a); ...
Re: Multidimensional Array
by toolic (Bishop) on Apr 15, 2010 at 13:09 UTC
    @a_a = (\a_4, \a_3); @a_b = (\@a_2, \a_a);
    You did not use references to your a_4, a_3 and a_a arrays. You probably meant to do this:
    | | V V @a_a = (\@a_4, \@a_3); @a_b = (\@a_2, \@a_a); ^ |

    You would have had a clue something was wrong had you use strict and warnings.

Re: Multidimensional Array
by choroba (Cardinal) on Apr 15, 2010 at 13:14 UTC
      0_0
      You guys are fast.
      Thank you everybody
      Thank you toolic
      Thank you Anonymous Monk
      Thank you choroba

      You four have saved my day.
Re: Multidimensional Array
by Anonymous Monk on Apr 15, 2010 at 13:10 UTC
    use Data::Dump::Streamer; @a_1 = (); @a_2 = (); @a_3 = (); @a_4 = (); @a_a = (\a_4, \a_3); @a_b = (\@a_2, \a_a); @a_final = (\@a_1, \@a_b); Dump(\@a_final); __END__ $ARRAY1 = [ [], [ [], \do { my $v = 'a_a' } ] ];
    Use strict warnings and diagnostics or die
Re: Multidimensional Array
by burgerw (Initiate) on Apr 15, 2010 at 13:26 UTC
    0_0
    You guys are fast.
    Thank you everybody
    Thank you toolic
    Thank you Anonymous Monk
    Thank you choroba

    You four have saved my day.