Re: for loop is not going to all array elements



Brandon Hoppe <bhoppe@xxxxxx> wrote in comp.lang.perl.misc:
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 ,Data***,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
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
.


Quantcast