$#arabian_nights = 1000;
This will tell perl that you want an array @arabian_nights of size 1001, but it won't prevent perl from adding elements past the pre-grown size. You can always truncate the array to the size you want, also by assigning to its last-index variable. (In other words, the line above sets the size of the array; whether this amounts to "pre-growing" or "truncating" depends on the size of the array at the time.)
BTW, you can "pre-grow" a hash too, by assigning to keys:
keys %mongo = 1_000_000;
Pre-growing, both for arrays and hashes, makes perl more efficient, since it doesn't need to jump through memory reallocation hoops every time the array or hash grows past the space allocated to it by default.
| [reply] [d/l] [select] |
| [reply] |
I think the general sentiment here is...
Write clear, easily maintainable code. If, when you've finished your program, you find that it runs too slowly you can then easily do stuff like this to go back and make it run a bit faster. | [reply] |
I don't think you're going to gain any efficiency or clarity from declaring it as such. I would just make a comment when you declare the array
#@large will hold a 1000 element list
my @large;
rather than
#@large will hold a 1000 element list
my @large;
$#large = 1000;
| [reply] [d/l] [select] |
# define $#array
@a = 1..5;
print scalar @a, "\n";
$#a++ for 1..5;
print scalar @a, "\n";
| [reply] [d/l] |
sh1tn, $#a++ for 1..5; is basically the same as
for (1..5)
{
$#a=$_;
}
what makes no sense in my eyes. Why assigning 1..4 when you end up with 5 and do nothing in between?
Anyway, here's mine:
my @a = (0) x 5
| [reply] [d/l] [select] |
Yes, you are right holli.
I ment to show what happens with each iteration.
Everyone has the right to dislike my style.
| [reply] |