Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

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

by davido (Cardinal)
on Jan 28, 2020 at 21:43 UTC ( [id://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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11111993]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2024-04-16 15:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found