puterboy has asked for the wisdom of the Perl Monks concerning the following question:
I have a large hash that I am using to cache an even larger number of data items.
When I need to add a new cache item, and the cache is full, I use 'each %hashCache' to select a "random" (key, value) pair which I then delete using 'delete($hashCache{$key})'
However, every once in a while, I get an error message:
Use of uninitialized value $key in delete...even though there are clearly still keys in the hash. If however, after every delete, I do something like '$mycount=keys(%hashCache)' then the error doesn't occur.
Thus it seems like unless I do something to tell perl to recalculate the keys, 'each' ends up sometimes giving up the same key that I had previously deleted. Is this a bug, limitation, or feature in the implementation of 'each' and/or how hash memory is cleaned up after 'delete'? (or am I truly missing something obvious).
|
|---|