Re: perl grammer help



On Fri, Feb 27, 2009 at 23:12, <mysticpine@xxxxxxxxx> wrote:
I have a input file ,which has begin ... end patterns
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
....
}
}
snip


#!/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.
.



Relevant Pages

  • Re: Git-commits mailing list feed.
    ... > Because then you have all those special cases with fetching them and with ... grabbing missing commits, and from there fetching any missing tree and ... And any form of validation of a tag is a special operation. ... send the line "unsubscribe linux-kernel" in ...
    (Linux-Kernel)
  • Re: Some img tages being stripped out in ASP.NET 2.0
    ... Hi Nick, ... I have the same problem with an Adrotator. ... browser locally but SOMETIMES the tag is completely missing when ...
    (microsoft.public.dotnet.framework)
  • RE: Setting Button Disabled Property to TRUE or FALSE
    ... we try to change the tag definition in the ... They just terminate and stay resident." ... "glenn" wrote: ... Seems like this should be a no brainer but maybe I'm missing something... ...
    (microsoft.public.scripting.vbscript)
  • Re: Caterham seven - "poseurs cafe racer"?
    ... impractical enough to not earn that kind of tag - or am I missing some secret sneer value? ... speeding across the frozen tundra. ...
    (uk.rec.cars.modifications)
  • Re: Guest Book mess
    ... I noticed that your site page is missing the tag which may be your problem. ... Webmaster ... Sayville, NY ...
    (microsoft.public.frontpage)