in reply to Function to measure size of a hash

Just wondering if there is a module or something out there in PERL that can be used to get the number of bytes in a hash
I'm guessing you mean the number of bytes in all the values of a hash, which you could do like so
my %hash = qw(foo one bar two baz three); my $hash_len = 0; $hash_len += length for values %hash; print "hash length is $hash_len\n"; __output__ hash length is 11
And make sure you use the bytes pragma if you're working with Unicode (unless you're using 5.8.0 which should deal with it seamlessly).
HTH

_________
broquaint

Replies are listed 'Best First'.
Re: Re: Function to measure size of a hash
by simeon2000 (Monk) on Jul 24, 2002 at 15:35 UTC
    Good solution... however, what if it's a multi-dimensional hash? Perhaps a recursive function that'll traverse the tree and find the number of bytes in each branch and eventually return it to the top?

    However, I believer perl requires quite a few bytes extra for storing each scalar, as well as even more bytes to keep track of hash mappings, so if the purpose of this proposed chunk of code is to help work out memory constraint on a system, the value it returns will be optimistically low at best.

    "Falling in love with map, one block at a time." - simeon2000