Re: for loop is not going to all array elements
- From: "A. Sinan Unur" <1usa@xxxxxxxxxxxxxxxxxxx>
- Date: Wed, 29 Mar 2006 23:23:08 GMT
Brandon Hoppe <bhoppe@xxxxxx> wrote in news:e0f40o$7nc$1
@home.itg.ti.com:
I have a for loop that loops that an array. Inside the for loop, I
push more elements onto the array if needed, but the loop is stopping
at the original end of the array and not looping thru the new
additions.
Well, don't do that.
From perldoc perlsyn, "Foreach Loops":
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 would question the need to modify an array while looping over it using
a foreach loop.
On the other hand, you should be able to use a C-style for loop where
you test against the array size in each iteration:
for (my $i = 0; $i < @array, ++$i ) {
# do something with $array[$i]
}
You'll need to make sure this does not turn into an infinite loop.
Sinan
--
A. Sinan Unur <1usa@xxxxxxxxxxxxxxxxxxx>
(remove .invalid and reverse each component for email address)
comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
.
- References:
- for loop is not going to all array elements
- From: Brandon Hoppe
- for loop is not going to all array elements
- Prev by Date: for loop is not going to all array elements
- Next by Date: Re: Passing user data between scripts
- Previous by thread: for loop is not going to all array elements
- Next by thread: Re: for loop is not going to all array elements
- Index(es):
Relevant Pages
|