I want to make an multidimensional array, but as i will declare it, i don't know how many element it will have, so I tried this:
my @multarray = [ ][ ];
Is it good?
Perl isn't C, and you don't need to initialise any sort of array. Just my @multarray is fine.
Observe
use strict;
use warnings;
my @data;
$data[2][2] = 99;
print $data[2][2], "\n";
The section in perldoc perlol on Declaration and Access of Arrays of Arrays will be of help here.
output
99
Perl doesn't support multidimensional arrays directly; an array is always just a sequence of scalars. But you can create references to arrays, and store the references in arrays, and that allows you to simulate multidimensional arrays.
The square bracket notation is useful here to create an array literal and return a reference to it. For example, the following creates an array of ('a','b',1234) and returns a reference to it:
my $ref_array = ['a','b',1234];
Here's an example of how to create (what we might call) a multidimensional array literal, of dimension 3x2:
my $multarray = [['a','b'],['c','d'],['e','f']];
So you could access the 'c' (for example) with:
print($multarray->[1]->[0]);
Now, you say you don't know what the final dimensions will be. That's fine; in Perl, you can conceptually view arrays as being infinite in size; all elements that haven't been assigned yet will be returned as undef, whether or not their index is less than or equal to the greatest index that has thus far been assigned. So here's what you can do:
my $multarray = [];
Now you can read and write directly to any element at any index and at any depth level:
$multarray->[23]->[19234]->[3] = 'a';
print($multarray->[23]->[19234]->[3]); ## prints 'a'
The syntax I've been using is the "explicit" syntax, where you explicitly create and dereference all the array references that are being manipulated. But for convenience, Perl allows you to omit the dereference tokens when you bracket-index an array reference, except when dereferencing the top-level array reference, which must always be explicitly dereferenced with the -> token:
$multarray->[23][19234][3] = 'a';
print($multarray->[23][19234][3]); ## prints 'a'
Finally, if you prefer to work with array variables (unlike me; I actually prefer to work with scalar references all the time), you can work with a top-level array variable instead of an array reference, and in that case you can escape having to use the dereference token entirely:
my @multarray;
$multarray[23][19234][3] = 'a';
print($multarray[23][19234][3]); ## prints 'a'
But even if you use that somewhat deceptively concise syntax, it's good to have an understanding of what's going on behind-the-scenes; it's still a nested structure of array references.
int a[3][2] (in C/C++) or String[][] (in Java). But your point is well-taken.
my @multarrayis fine