Dieter D'Hoker said:
if i have an aray @array that i'm going to fill :
for (my $y=0; $y<20; $y++) {
for (my $x=0; $x<20; $x++) {
$field[$x][$y] = 0;
}
}
Two problems:
1) You're referring to @field here, not @array. Which is it?
2) C-style loops are ugly. Assuming you are going to do
something like this, try a more Perlish approach:
foreach my $x (0..19) {
foreach my $y (0..19) {
$field[$x][$y] = 0;
}
}
should i use $#array = 19;
or $#array = 399; ?
to predefine the correct lenght ?
The answer is neither, as a rule. Why do you feel you need to
predefine the length of the array at all? I suppose if you had
several thousand entries it might make sense, but for the example you
posted, it's a waste of time. So far, I have never written a program
in Perl that would benefit significantly from preallocating arrays,
and I've been coding in Perl for about 7 years now.
That said, if you absolutely feel you must, and I emphasize again that
you probably do not need to do this, the answer lies in what a '2d
array' really is in Perl: an array of arrayrefs. So the top-level
array only should be predefined to have as many entries in it as $x
will index.
-=Eric