in reply to What went wrong with this change in code?
Once you've read a line from <IN>, it is "used up", and the next call to <IN> will return the line after it.
So when you do
while ($line = <IN>) {$nextline=<IN>;
You're reading two lines, and so overall $nextline will only contain every second line in the file (all even-numbered lines if you start counting from 1).
So if all the matches are on odd-numbered lines, you won't see anything ini the output.
Try something like this instead:
my $previous = ''; while ($line = <IN>) { if ($line =~ /mamma/ && $line =~ /mia/) { print OUT $previous; print OUT $line; } else { print "."; } } continue { $previous = $line; }
That way $line sees all the lines in the input file, and you still have the previous line available inside the loop.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: What went wrong with this change in code?
by naturalsciences (Beadle) on Nov 10, 2011 at 11:29 UTC |