in reply to Modifying 'next' in foreach loop
One way is to loop over the indices of the array rather than the array items.
for( my $i = 0; $i <= $#lines; $i++ ) { if ( $lines[$i] =~ s/^SY(?= )/CN/ ) { if ( defined $lines[$i+1] ) { $lines[$i+1] =~ s/^SY(?= )/ /; } } }
Another way is to join all the lines into one large string and do replacements.
Still another way would be to use a flag variable to say when the previous line matched.
my $previous_line_matched = 0; foreach my $line ( @lines ) { if ( $previous_line_matched ) { $line =~ s/^SY(?= )/ /; } if ( $line =~ s/^SY(?= )/CN/ ) { $previous_line_matched = 1; next; } $previous_line_matched = 0; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Modifying 'next' in foreach loop
by GrandFather (Saint) on Apr 28, 2008 at 19:38 UTC |