in reply to Array Processing

Change the foreach to a while. You're not using the itteration value anyway.

I don't understand why, but the problem is to do with the way the @a is evaluated on each pass through the loop. The behaviour may be version dependent even.


Perl is Huffman encoded by design.

Replies are listed 'Best First'.
Re^2: Array Processing
by davido (Cardinal) on Oct 08, 2005 at 08:22 UTC

    This doesn't exactly answer why, but the behavior is documented in perlsyn:

    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.

    As for "why", I think that's best answered with a question: "What do you think should happen if you remove elements as you iterate over them?" Since that question has no good answer, the answer to 'why' is "Because there's no good single answer to the question of what should happen."


    Dave