"Perl Programmers Reference Guide (англ.) (программ.) /19.12.1998/ " - читать интересную книгу автора If you evaluate a named array in a 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 $[: files that don't set the value of $[ no longer need to worry about whether another file changed its value. (In other words, use of $[ is deprecated.) So in general you can assume that scalar(@whatever) == $#whatever + 1; Some programmers choose to use an explicit conversion so nothing's left to doubt: $element_count = scalar(@whatever); If you evaluate a hash in a scalar context, it returns a value that is true if and only if the hash contains any key/value pairs. (If there are any key/value pairs, the used buckets and the number of allocated buckets, separated by a slash. This is pretty much useful only to find out whether Perl's (compiled in) hashing algorithm is performing poorly on your data set. For example, you stick 10,000 things in a hash, but evaluating %HASH in scalar context reveals "1/16", which means only one out of sixteen buckets has been touched, and presumably contains all 10,000 of your items. This isn't supposed to happen.) You can preallocate space for a hash by assigning to the 12 perl 5.005, patch 02 14/Jun/98 PERLDATA(1) Perl Programmers Reference Guide PERLDATA(1) _k_e_y_s_(_) function. This rounds up the allocated bucked to the next power of two: |
|
|