use List::Util 'reduce'; sub hash_char_count :prototype(\%) { my $href = shift; return reduce {$a += length $b} 0, %$href; } my %hash = (foo => 'bar', baz => 1, fiddle => 'faddle'); print hash_char_count(%hash), "\n";
I like List::Util::reduce for this. It's almost too trivial to bother with a subroutine around it, except that you get to give subroutines descriptive names so people don't have to puzzle over what it's doing.
Sorry for the use of a prototype. Here's without:
# ... sub hash_char_count { # ... } # ... print hash_char_count(\%hash), "\n";
Dave
In reply to Re: Get total number of characters in a hash (not memory usage)
by davido
in thread Get total number of characters in a hash (not memory usage)
by TieUpYourCamel
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |