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

In reply to Re: If line is followed by line, delete both lines by Marshall
in thread If line is followed by line, delete both lines by Ganesh Bharadwaj1

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.