joe joe - 1 year ago 79
Perl Question

How can I find the number of keys in a hash in Perl?

How do I find the number of keys in a Perl hash variable, like Perl array


Answer Source
scalar keys %hash

or just

keys %hash

if you're already in a scalar context, e.g. my $hash_count = keys %hash  or  print 'bighash' if keys %hash > 1000.

Incidentally, $#array doesn't find the number of elements, it finds the last index. scalar @array finds the number of elements.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download