Perl : Get Number of elements from hash

How to get the number of elements a Perl hash holding?

Using the keys() function of Perl, you can easily get the count. The function used to return all the keys from the hash as array. But if you use a SCALAR variable, that case it will return you the element count from the hash.

my %hash = (red => 1, blue => 2, green => 3);
my $count = keys(%hash); ## Scalar requested.
print $count;


Is there any other method to get the elements count from a Perl hash? I mean not using a loop.

Comments

Anonymous said…
try %#hash