How can I know how many entries are in an associative array?
While the number of elements in a @foobar array is simply @foobar when
used in a scalar, you can't figure out how many elements are in an
associative array in an analogous fashion. That's because %foobar in
a scalar context returns the ratio (as a string) of number of buckets
filled versus the number allocated. For example, scalar(%ENV) might
return "20/32". While perl could in theory keep a count, this would
break down on associative arrays that have been bound to dbm files.
However, while you can't get a count this way, one thing you *can* use
it for is to determine whether there are any elements whatsoever in
the array, since "if (%table)" is guaranteed to be false if nothing
has ever been stored in it.
So you either have to keep your own count around and increments
it every time you store a new key in the array, or else do it
on the fly when you really care, perhaps like this:
$count++ while each %ENV;
This preceding method will be faster than extracting the
keys into a temporary array to count them.
As of a very recent patch, you can say
$count = keys %ENV;