Re: for loop is not going to all array elements



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

.



Relevant Pages

  • Performance test: for + foreach
    ... except one used a "for" loop and the other used "foreach" loop. ... I had a problem whereby I had a string that I wanted to cut to x number of ... array, take each array element and append it to a new ... or a foreach loop when both were required to perform the ...
    (comp.lang.php)
  • RE: Error 3021
    ... Create proto-file names using the selected job names and storre to an array ... Save and close the document and repeat the loop ... Dim strJobsAs String, strDocsAs String, varValsAs _ ...
    (microsoft.public.access.modulesdaovba)
  • RE: Error 3021
    ... Kevin Backmann ... Create proto-file names using the selected job names and storre to an array ... Save and close the document and repeat the loop ... Dim strJobsAs String, strDocsAs String, varValsAs _ ...
    (microsoft.public.access.modulesdaovba)
  • RE: Error 3021
    ... Create proto-file names using the selected job names and storre to an array ... Save and close the document and repeat the loop ... Dim strJobsAs String, strDocsAs String, varValsAs _ ...
    (microsoft.public.access.modulesdaovba)
  • Re: Displaying a large amount of data quickly (VB6)
    ... >>> involving a loop of VB code would be too slow. ... but I'd sure be interested to know if that StringBuilder ... Array elements: 25000 ... Array construction: 17 ...
    (microsoft.public.vb.controls)