Re: for loop is not going to all array elements
- From: anno4000@xxxxxxxxxxxxxxxxxxxxxxx (Anno Siegel)
- Date: 30 Mar 2006 09:19:12 GMT
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.
.
- 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: Re: Where to put Settings-Variables?
- Next by Date: Re: Arbitrarily Many Nested Loops
- Previous by thread: Re: for loop is not going to all array elements
- Next by thread: Arbitrarily Many Nested Loops
- Index(es):