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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|