in reply to If line is followed by line, delete both lines
this reproduces your desired printout. You tell me how close this is to what you really want...
update: But if dog is followed by cat, I want to keep both the lines. You desired output shows the reverse, e.g. cat following dog causes the 2 lines to be deleted.
The basic idea below (with variance for other implementation variations). The basic idea is that it is "hard" to "undo a write" (go backwards and say "oh, I didn't mean that). Keep track of stuff until you are sure that you want to keep it, then write it out. Once it is gone, you can forget about it and start thinking about the next pair of things.
I suspect that you will need to generate a better thought out example if this code doesn't do what you want.
#/usr/bin/perl use strict; use warnings; my @lines; while (my $cur_line = <DATA>) { push @lines, $cur_line; next if @lines <2; print @lines unless ($lines[0] =~ /dog/ and $lines[1] =~ /cat/); @lines = (); } =prints dog dog cat goat =cut __DATA__ dog cat dog dog cat goat
|
|---|