in reply to Mutable foreach list?

It says on the label "don't muck with arrays you're looping over", you muck with the array, and you get strange behavior. What's to not understand?

While I concede it might be interesting to try and figure out what execution path is happening to cause the results, you're in doctor-it-hurts-when-I-do-this territory and shouldn't count on any particular behavior being repeatable or stable.

Replies are listed 'Best First'.
Re^2: Mutable foreach list?
by zigdon (Deacon) on Jan 11, 2007 at 15:57 UTC
    Right - I'm on forbidden ground. Perlsyn says:
    If any part of LIST is an array, "foreach" will get very confused if you add or remove elements within the loop body, for example with "splice". So don’t do that
    I guess what's happening is something like this (psudocode):
    $it = &make_list_into_iterator(LIST); $count = $it->figure_out_element_count; while ($count-- > 0) { $_ = $it->next; CODE }
    Which fits the behaviour I see.

    -- zigdon