for loop is not going to all array elements

B

Brandon Hoppe

Hi,

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.

Basicall, this is what I have:

$line = "-name SSCC34234342 -views ,Datasheet,Verilog!BREAK!";
@wow = ();
push(@wow, $line);

foreach $inline (@wow) {
print "LINE: $inline\n";

if($inline =~ /SSCC/) {
$newline = $inline;
$newline =~ s/SSCC/BRGS/;
push(@wow, $newline);
}
}

Now this code above works. It prints the line with SSCC and then prints
the line with BRGS.

But in a large piece of code, I have a similar loop. Several other
things happen inside the loop, so I'm not sure what could be wrong.

As a debug method, just before the end brace of the foreach loop I added
a simple foreach loop that prints out all the elements of the @wow
array. This prints out all the newly added elements and the original
array. So in the case above it prints out the SSCC line and the BRGS
line. So the array is correctly updated from what I take, its just that
the original foreach loop isn't going to the new element.

Any ideas? I've tried to add as much info as possible. The loop is
pretty large and part of a cgi script so its hard to cut out and test
just the for loop with a debugger.
 
A

A. Sinan Unur

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 <[email protected]>
(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
 
A

Anno Siegel

Brandon Hoppe said:
Hi,

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.

No surprise there. Read up on "foreach" in perlsyn. It explicitly
warns against what you are doing.
Basicall, this is what I have:

$line = "-name SSCC34234342 -views ,Datasheet,Verilog!BREAK!";
@wow = ();
push(@wow, $line);

foreach $inline (@wow) {
print "LINE: $inline\n";

if($inline =~ /SSCC/) {
$newline = $inline;
$newline =~ s/SSCC/BRGS/;
push(@wow, $newline);
}
}

You're running without strictures (and probably without warnings).
Switch them on. They don't make a difference here, but they make
your code easier to check.

Use a while loop instead:

while ( @wow ) {
my $inline = shift @wow;
# ...
}

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top