in reply to Re: foreach loop question
in thread foreach loop question

"You are correct - a foreach loop builds a temporary list, and iterates over that." I don't think this is true. Try this:
my @list = (1,2,3,4,5); foreach (@list) { print $_."\n"; push @list,$_.$_; }
This makes an infinite loop (at least until your memory runs out), so it appears that foreach is actually using the original list

Replies are listed 'Best First'.
Re: Re: Re: foreach loop question
by Mr. Muskrat (Canon) on Apr 16, 2003 at 22:06 UTC

    This 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.