in reply to Re: Hash optimization in Perl?
in thread Hash optimization in Perl?

Are you sure keys is an lvalue function?

I get no complaint in scalar assignment, but no indication it worked, either. List assignment gives,

$ perl -Mwarnings -Mstrict -e'keys( my %foo) = qw/foo bar baz/;print %foo'
Useless use of a constant in void context at -e line 1.
Useless use of a constant in void context at -e line 1.
Argument "baz" isn't numeric in scalar assignment at -e line 1.
$ 

After Compline,
Zaxo

Replies are listed 'Best First'.
Re^3: Hash optimization in Perl?
by Anonymous Monk on May 02, 2007 at 07:33 UTC
    perldoc -f keys
    As an lvalue "keys" allows you to increase the number of hash buckets allocated for the given hash. This can gain you a measure of efficiency if you know the hash is going to get big. (This is similar to pre-extending an array by assigning a larger number to $#array.) If you say
    keys %hash = 200;
    then %hash will have at least 200 buckets allocated for it--256 of them, in fact, since it rounds up to the next power of two. These buckets will be retained even if you do "%hash = ()", use "undef %hash" if you want to free the storage while %hash is still in scope. You can't shrink the number of buckets allocated for the hash using "keys" in this way (but you needn't worry about doing this by accident, as trying has no effect).