|
hi alan --
In a message dated 4/9/2006 12:41:23 P.M. Eastern Standard Time,
[EMAIL PROTECTED] writes:
> I went netsearching for this but couldn't find anything in all the
noise.
> > 1) What is the runtime cost of the scalar operator? Is it constant > or does it depend on the size of the array? Is it different than $#array? it IS different. per perldata:
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. (It
used to do so in Perl 4, but we had to break this to make sure destructors were
called when expected.)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;
If you evaluate an array in scalar context, it returns the length of the array. (Note that this is not true of lists, which return the last value, like the C comma operator, nor of built-in functions, which return whatever they feel like returning.) The following is always true: scalar(@whatever) == $#whatever - $[ + 1; Version 5 of Perl changed the semantics of scalar(@whatever) == $#whatever + 1; Some programmers choose to use an explicit conversion so as to leave nothing to doubt: $element_count = scalar(@whatever); > assuming all these variations are syntactically correct, i would prefer A as being more maintainable. if it is a question of which is faster, benchmarking will tell the answer. > i'm not sure just what question 3 refers to.
hth -- bill walters
|
_______________________________________________ ActivePerl mailing list [email protected] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
