Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Get total number of characters in a hash (not memory usage)

by davido (Cardinal)
on Jan 28, 2020 at 21:43 UTC ( #11111993=note: print w/replies, xml ) Need Help??


in reply to Get total number of characters in a hash (not memory usage)

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11111993]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2020-06-05 04:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?



    Results (35 votes). Check out past polls.

    Notices?