in reply to Modifying 'next' in foreach loop

Anal-retentive comments: I think you meant $next, not next. You forgot to show us how $next was set.

Given the way you're processing the file, I think you need to include a state indicator.

my $changed = 0; foreach $line (@lines) { if ($line =~m/^SY /) { if ( $changed == 0 ) { $line =~ s/^SY/CN/) $changed = 1; } else { $line =~ s/^SY/ /; $changed = 0; } } else { $changed = 0; } }
There are terser ways of representing this, but I think this is readable. (It is untested, be warned!)