-4

After introducing:

use strict;

My 2-dimensional array do not work any more, and I find no description and no example in the documentation how to predeclare them. So, what have I to do?

Minko Gechev
  • 25,304
  • 9
  • 61
  • 68
ubuplex
  • 189
  • 1
  • 5
  • 2
    How did you declare it before you did `use strict;`? In Perl you can't really say, "hey, I have an array and it's two dimensional." All you can say is, "hey, I have an array, here it is: `my @array = ();`. Then you can start putting array references in it two make it two dimensional on the fly. – Andrejovich Feb 06 '14 at 12:24
  • 8
    Also, "it doesn't work" isn't a problem description. Do you get any error messages? Any warnings? (Do you `use warnings;`, btw?) What output do you expect? What output do you actually get? **Provide details. Share your research.** – Andrejovich Feb 06 '14 at 12:27
  • Before `use strict` it was not predeclared at all. So an access `$a[$i][$j]` was no problem. When it was written the first time, it was written, when it was read the first time, it was read. Now, on the very first access, I get of course `Global symbol "@zelle" requires explicit package name at ConwaysSpiel.pl line 23` and I dot know how to fix this. The array should be a global variable because it holds the playground of Conway's life simulation. – ubuplex Feb 06 '14 at 12:34
  • 2
    @ubuplex In that case, simply declare your variable at the top: `my @zelle;`. While that isn't stellar design either, it should solve your immediate problem. – amon Feb 06 '14 at 12:42
  • Oooof, that's all too easy. I had tried that, but got syntax error messages. So there must have been other reasons that somehow are eliminated now. Works now. – ubuplex Feb 06 '14 at 12:51

2 Answers2

2

It's impossible to give much useful help from the details that you give. Just including the text of the error message would have been useful.

In general, when you have an error that you don't understand, it's a good idea to add use diagnostics to your code. That will give you longer explanations of any errors and warnings that are generated.

(But that should be seen as a development tool - you shouldn't leave it in the code when it goes into production.)

Dave Cross
  • 68,119
  • 3
  • 51
  • 97
1

You declare it in just the same way you would a 1D array:

use strict;
use warnings;

my @AoA =  ( ['a', 'b'], ['foo', 'bar'] );

print Dumper \@AoA;

$VAR1 = [
          [
            'a',
            'b'
          ],
          [
            'foo',
            'bar'
          ]
        ];
fugu
  • 6,417
  • 5
  • 40
  • 75