Thursday, May 28, 2009

Perl : Get Number of elements from hash


Share at Facebook

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.




1 comment:

Anonymous said...

try %#hash