in reply to If line is followed by line, delete both lines

I'm not quite sure what you want.
Looks like you are processing the input DATA as pairs of lines. You print both lines of the pair unless the specific "dog followed by cat" line sequence of the pair occurs? This is a bit different than deleting 2 lines if "cat" just happens to follow just any "dog". That is possible, just doesn't appear to be what you are asking for.

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