Re: perl grammer help
- From: chas.owens@xxxxxxxxx (Chas. Owens)
- Date: Sun, 1 Mar 2009 10:58:05 -0500
On Fri, Feb 27, 2009 at 23:12, <mysticpine@xxxxxxxxx> wrote:
I have a input file ,which has begin ... end patternssnip
some of the records are not delimitted end missing,
How do I separate records that dont have BEGIN and END ,
and collect informtion of what ever date ,thats in the record
two lines of data for each record,could be more lines
Write perl grammer tokenise the records ?
open(IN,"<iostat.txt");
while(<IN>){
if(/^BEGIN/ .. /^END/){
...
...collect the record
....
}
}
#!/usr/bin/perl
use strict;
use warnings;
$/ = "END\n";
while (<DATA>) {
chomp; #remove END tag
#we can only detect missing BEGIN tags after an END tag
#this also gets rid of the first BEGIN tag, so the split
#that follows will work better
print "no BEGIN tag on record $.\n" unless s/\ABEGIN\n//;
#If this split produces more than one record
#then we have missing END tags
my ($record, @extra_records) = split /^BEGIN\n/m;
print "no END tag on record $.\n" if @extra_records;
print "$record";
while (defined(my $record = shift @extra_records)) {
$.++; #we need to tell Perl we found another record
print "no END tag on record $.\n" if @extra_records;
print "$record";
}
}
__DATA__
BEGIN
rec1 good
END
BEGIN
rec2 missing end
BEGIN
rec3 good
END
BEGIN
rec4 good
END
BEGIN
rec5 good
END
rec6 missing begin tag
END
BEGIN
rec7 missing end tag (undetectable)
rec8 missing begin tag (undetectable)
END
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
.
- References:
- perl grammer help
- From: mysticpine
- perl grammer help
- Prev by Date: Re: module to work with csv files
- Next by Date: Re: word similarity measure
- Previous by thread: perl grammer help
- Next by thread: CPAN install module problem
- Index(es):
Relevant Pages
|