Re: for loop is not going to all array elements



In article <e0f40o$7nc$1@xxxxxxxxxxxxxxx>, Brandon Hoppe
<bhoppe@xxxxxx> wrote:

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 ,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);
}
}

Why don't you copy the array first, then iterate over the array adding
lines to the copy as needed (untested):

push(@wow, $line);
my @wow2 = @wow;

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

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

Do you need to reprocess the lines added?

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
.


Quantcast