in reply to Re^3: What's going on in array element assignment?
in thread What's going on in array element assignment?
This is documented in perldata#Scalar-values:
The length of an array is a scalar value. You may find the length of array @days by evaluating $#days, as in csh. However, this isn't the length of the array; it's the subscript of the last element, which is a different value since there is ordinarily a 0th element. Assigning to $#days actually changes the length of the array. Shortening an array this way destroys intervening values. Lengthening an array that was previously shortened does not recover values that were in those elements.
You can also gain some minuscule measure of efficiency by pre-extending an array that is going to get big. You can also extend an array by assigning to an element that is off the end of the array. You can truncate an array down to nothing by assigning the null list () to it. The following are equivalent:@whatever = (); $#whatever = -1;
(BTW, in my experience the speed-up to be gained by pre-allocating array memory is sometimes far from “miniscule.”)
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: What's going on in array element assignment?
by zapdos (Sexton) on Aug 12, 2020 at 04:37 UTC | |
by Athanasius (Archbishop) on Aug 12, 2020 at 06:24 UTC | |
by zapdos (Sexton) on Aug 12, 2020 at 06:33 UTC |