OK I experimented a little with
eof, and I found
eof (no handle, no parens) returns true on the last line. So If I extend the end side of the flipflop operator with an extra test on
eof, and set a flag to true only if it succeeds because of this extra test, then I can safely check for the last match, too. I do need to modify the regex a little to allow for this, too.
#! perl -w
my $buffer;
LINE: while(<DATA>) {
my $eof; # flase by default
if(my $counter = (/^name\s.*\{/ ... (/^name\s.*\{/ || ($eof = eof)
+))) {
if($counter == 1) { # begin
$buffer = '';
}
$buffer .= $_;
if($counter =~ /E/) { # end
if($buffer =~ /name(.*?)(^name|\z)/ms) {
print "Found: $1\n";
}
redo LINE unless $eof; # retry with same line, unless en
+d condition is BECAUSE OF eof
}
}
}
__DATA__
name value {
test 1;
test 2;
test 5;
}
name three {
eat 4;
eat 5;
}
name four {
eat 6;
eat 7;
}
Result:
Found: value {
test 1;
test 2;
test 5;
}
Found: three {
eat 4;
eat 5;
}
Found: four {
eat 6;
eat 7;
}
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.