[Chicago-talk] Odd HoH bug/problem

Shawn Carroll shawn.c.carroll at gmail.com
Tue Sep 11 12:10:38 PDT 2012

On Tue, Sep 11, 2012 at 1:53 PM, Randal L. Schwartz
<merlyn at stonehenge.com> wrote:
>>>>>> "Shawn" == Shawn Carroll <shawn.c.carroll at gmail.com> writes:
> Shawn> I hit send to soon...
> Shawn> On Tue, Sep 11, 2012 at 1:33 PM, Shawn Carroll
> Shawn> <shawn.c.carroll at gmail.com> wrote:
>>> Let me preface this by saying that I have not been able to reproduce
>>> this problem in a smaller program and I understand that this makes it
>>> less useful for identifying the root cause.
>>> In my program I create a HoHoH and need to step through in a final
>>> sub.  As I've done many times in the past I use a while block and use
>>> each to assign the key & value to loop variables.
>>> while( my ($id, $types) =  each %data )
>>> {
>>> say "$id => $types";
>>> while ( my ($type, $data) = each %{ $types })
>>> {
> Shawn>                        say $type
> Shawn>                        # processData
> Shawn>                   }
> Shawn>            }
> Shawn> The first say confirms that every $types is a hash but the second
> Shawn> while loop doesn't process every $types.  If I replace the second
> Shawn> while with a foreach my $type (keys %{$types}) loop everything works
> Shawn> as expected.
> Are you adding or deleting things from %$types during the each
> iteration?  If so, that can mess up your day.
> Is anything else also walking the hash (like something calling keys,
> values, or each as well)?  There's only one each-style iterator per
> hash.
> --
> Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
> <merlyn at stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
> Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
> See http://methodsandmessages.posterous.com/ for Smalltalk discussion

No adding or deleting from %$types nor is anything else walking either hash.

While I was debugging this I had say Dumper statements, to confirm the
data I was expecting was there, to dump the contents of %$types before
the while inner loop and everything would work as expected.  W/o it I
wouldn't get all my data.  The inputs all stayed the same between each
run and I am not doing anything funky w/ the data, just pulling out
what I need for output.  I even confirmed after the outer loop that
the data I wanted was still there.

I am happy I have a fix, but I'd love to understand what I did wrong.
Tonight if I have time I will see if I can reproduce on a smaller set
that I can share w/ the world but I'm not holding out hope.

More information about the Chicago-talk mailing list