G'day Ganesh Bharadwaj1,
Below is my take on a solution.
I did some additional testing as follows (all successful):
-
I originally used your data and got your expected output.
-
I then added "test 1" and got your expected output again.
-
I then added "test 2a" and "test 2b": your expected output again.
-
I then added "test 3": your expected output again.
-
I then added "test 4": your expected output PLUS the RRRRR line at the end.
#!/usr/bin/env perl
use strict;
use warnings;
my ($skip, @buf);
while (<DATA>) {
if ($skip) {
$skip = 0;
next;
}
if (/XXXXX/) {
@buf = ();
$skip = 1;
}
else {
push @buf, $_;
$skip = 0;
}
if ($. > 1 && @buf >= 2) {
print for @buf[0 .. $#buf - 1];
@buf = ($buf[-1]);
}
}
print for @buf;
__DATA__
XXXXX - additional test 1
PPPPP
XXXXX is my name
YYYYY
KKKKK
UUUUU
BBBBB
CCCCCC
XXXXX is what I play
KKKKK
NNNNN
ZZZZZ - additional test 2b
XXXXX - additional test 2a
QQQQQ - additional test 3
RRRRR - additional test 4
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.