Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > for loop is not going to all array elements

Reply
Thread Tools

for loop is not going to all array elements

 
 
Brandon Hoppe
Guest
Posts: n/a
 
      03-29-2006
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.
 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      03-29-2006
Brandon Hoppe <(E-Mail Removed)> 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 <(E-Mail Removed)>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc...uidelines.html

 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      03-30-2006
Brandon Hoppe <(E-Mail Removed)> 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 ,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
--
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
XP Going ... Going ... Not Gone Lawrence D'Oliveiro NZ Computing 0 06-11-2010 01:41 AM
If-Else within a loop going through array elements... Saeed Bhuta Ruby 5 04-22-2010 01:40 PM
XP search going all lame on me..explorer going crazy when trying tosearch. fotoobscura Computer Support 8 01-12-2008 01:09 AM
To allow left click only on some elements of web page NOT all elements vikas.khengare@gmail.com Javascript 1 05-31-2006 09:26 AM



Advertisments