My perl (AS 5.61 on Windows XP) also dies, but not every time. Using diagnostics, I get the following message, among others:

Attempt to free unreferenced scalar at scratchpad2.pl line 5 (#2): (W internal) Perl went to decrement the reference count of a scalar to see if it would go to 0, and discovered that it had already gone to 0 earlier, and should have been freed, and in fact, probably was freed. This could indicate that SvREFCNT_dec() was called too many times, or that SvREFCNT_inc() was called too few times, or that the SV was mortalized when it shouldn't have been, or that memory has been corrupted.

which may help some, but I'm afraid is a little over my head.

Concerning the third antirice loop:

foreach my $key ( @{[%hash]} ) { # print "$key: @{[%hash]}\n"; delete $hash{$key}; }
I thought it looked to be (conceptually) equivalent to something like:

foreach my $key (my @ary = %hash) { # print "$key: @ary\n"; delete $hash{$key}; }

(which also 'works', BTW), but apparently it isn't the same, since uncommenting the print lines shows that the anonymous array reference (vocab a bit shaky here...) [%hash] shrinks by two items with each iteration, while the 'straight' array @ary remains unchanged.

Oh dear, not sure if all that helps at all...

dave


In reply to Re: Re: Re: Hashes: Deleting elements while iterating - deleting phantoms? by Not_a_Number
in thread Hashes: Deleting elements while iterating by knexus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.