[tpm] Schrodinger's hash ?

Uri Guttman uri at stemsystems.com
Fri Jun 22 11:27:15 PDT 2012


On 06/22/2012 01:36 PM, Fulko Hew wrote:
> I have a mysteriously disappearing hash...
>
> If I look at it (in advance) it contains stuff,
> but if I just 'use' it, then its empty... Schrodinger's ghost?
>
>
> I have the following fragment:
>
>     ...
>     #print "varbindTable num keys: ", scalar keys %varbindTable, "\n";
>     while (($index, $object) = each %varbindTable) {
>         ... do loopy stuff ...
>     }
>
> If I enable the print statement and run my code, the hash 'contains 
> stuff'.
> but if I comment it out, the while loop is skipped.
>
i feel this has something to do with the internal iterator of the hash. 
try calling keys again in void context as that will reset the iterator:

                 As a side effect, calling keys() resets the HASH's internal
                iterator (see "each").  In particular, calling keys() in 
void
                context resets the iterator with no other overhead.

somehow the first call to keys is iterating through and leaving it at 
the end of the hash (yes, end of found keys!). then the each call fails 
as you can see. a reset via keys should fix it. dunno why the scalar 
keys call leaves it in that state.

uri


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/toronto-pm/attachments/20120622/3e5327fb/attachment.html>


More information about the toronto-pm mailing list