I would still like learn how to implement the between range operator elegantly to handle this type of situation, but in the meantime I've rewritten the program to use a state variable (thanks
tadman for the suggestion). TIMTOWTDI:
use strict;
my @jobs = (
'file2.txt|CCC|EOCCC',
'file1.txt|SEGMENT3|EOS3',
'file3.txt|P333|EOP333'
);
for (@jobs) {
my ($file, $beg, $end) = split /\|/;
my $first_line = 1;
my $state = 0;
my @lines = ();
print "Opening file: '$file'\n",
" beg: '$beg' end: '$end'\n";
open (INFILE, "<$file")
or die "Could not open '$file': $!\n";
while (<INFILE>) {
chomp;
$state = 1 if (/$beg/);
if ($state) {
print " first: '$_'\n\n" if $first_line;
$first_line = 0;
push (@lines, $_);
}
$state = 0, last if /$end/;
}
## no matching /$end/, so add one
push (@lines, $end) if $state;
print "$_\n" for @lines;
print "-" x 30, "\n\n";
}
--Jim
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.