The classic post on this flip flop subject (now in the tutorial's section) is
Flipin good, or a total flop? by
Grandfather. Also see the post from
ysth in that thread about how to include or exclude the end points (START STOP). I use this every time I write flip/flop code to refresh my recollection.
Update: I personally like Athanasius's code. But here is another way to do it without the flip/flop operator... Here the "state" of inside record or not is handled by being inside the process_record subroutine or not. At some point, you may find this parsing technique of use...I added some junk to the data and moved the START line from the beginning. If you want the junk to be preserved, maybe these are comments? modify the first while loop. Here I deleted ignored it.
#!/usr/bin/perl
use strict;
use warnings;
while (my $line = <DATA>)
{
process_record($line) if $line =~ /START/;
}
sub process_record
{
my ($line) = @_;
my @lines;
my $found;
push @lines, $line; #the "START" line
while ( $line = <DATA>, $line !~ /END/)
{
$found = 1 if $line =~ /def/;
push @lines, $line;
}
push @lines, $line; #the "END" line
print @lines unless $found;
return;
}
=prints
START
xyz
abc
END
=cut
__DATA__
START
abc
def
ghi
END
junk
more junk
START
xyz
abc
END
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.