PDA

Click to See Complete Forum and Search --> : selecting between records


nmretd
Oct 17th, 2001, 08:07 AM
Using perl, I want to pick up only the records between Start and Finish in INFILE:

i.e

Start
AAH
BBH
CCH
Finish

I am using the following code but this only excludes Start and writes everything else to output.txt ?

my $sField = "Start";

foreach $_ (<INFILE> ) {
if ($_ !~ $sField) {
print OUTFILE;
}
}

JoshT
Oct 17th, 2001, 01:48 PM
Try this, I think it's a little clearer as to what's going on:
use strict;

my $infile = 'file.txt';
my $found = 0;

open(INFILE, $infile) or die "Can't open $infile: $!\n\n";

foreach my $line (<INFILE>) {
if ($line =~ /Start/) {
$found = 1;
} elsif ($line =~ /Finish/) {
$found = 0;
} elsif ($found == 1) {
print OUTFILE $line;
}

}