for loop is not going to all array elements

Discussion in 'Perl Misc' started by Brandon Hoppe, Mar 30, 2006.

  1. 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.
     
    Brandon Hoppe, Mar 30, 2006
    #1
    1. Advertising

  2. Brandon Hoppe <> 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 <>
    (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. Sinan Unur, Mar 30, 2006
    #2
    1. Advertising

  3. Brandon Hoppe

    Anno Siegel Guest

    Brandon Hoppe <> 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.
     
    Anno Siegel, Mar 30, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gerald Aichholzer
    Replies:
    2
    Views:
    2,550
    Gerald Aichholzer
    Jun 27, 2006
  2. Saeed Bhuta
    Replies:
    5
    Views:
    206
    Charles Johnson
    Apr 22, 2010
  3. Replies:
    1
    Views:
    130
  4. Replies:
    4
    Views:
    194
  5. Isaac Won
    Replies:
    9
    Views:
    397
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page