Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Help Parsing a Text File

by tcf22 (Priest)
on Mar 23, 2004 at 19:02 UTC ( [id://339121]=note: print w/replies, xml ) Need Help??


in reply to Help Parsing a Text File

Perhaps something like this is what you need
my @entries; my $entry; my $start_parsing = 0; while(<DATA>){ if(/\d-\d\d-ABC\d\d/){ push(@entries, $entry) if(defined $entry); $entry = ''; $start_parsing = 1; } next unless($start_parsing); $entry .= $_; } push(@entries, $entry) if(length($entry) > 0); #Last entry my $i = 0; foreach(@entries){ print "Entry #$i:\n$_\n"; $i++; } __DATA__ 1-11-ABC22 (12:12) ABC 12 Foo Bar Foo Bar Foo Bar Foo Bar 2-15-ABC33 (12:12) ABC 12 Foo Bar Foo Bar Foo Bar Foo Bar -- 1-11-ABC33 (12:12) ABC 12 Foo Bar Foo Bar Foo bar Foo Bar -- 1-11-ABC33 (12:12) ABC 12 Foo Bar Foo Bar Foo abr Foo bar Foo Bar Foo Bar Foo Bar -- 2-15-ABC45 (12:12) ABC 12 Foo Bar Foo Bar Foo Bar Foo Bar 3-33-ABC15 (12:12) ABC 12 Foo Bar Foo Bar

-----------------------

Output is
Entry #0: 1-11-ABC22 (12:12) ABC 12 Foo Bar Foo Bar Foo Bar Foo Bar Entry #1: 2-15-ABC33 (12:12) ABC 12 Foo Bar Foo Bar Foo Bar Foo Bar -- Entry #2: 1-11-ABC33 (12:12) ABC 12 Foo Bar Foo Bar Foo bar Foo Bar -- Entry #3: 1-11-ABC33 (12:12) ABC 12 Foo Bar Foo Bar Foo abr Foo bar Foo Bar Foo Bar Foo Bar -- Entry #4: 2-15-ABC45 (12:12) ABC 12 Foo Bar Foo Bar Foo Bar Foo Bar Entry #5: 3-33-ABC15 (12:12) ABC 12 Foo Bar Foo Bar
Update: Took into account leading lines to be ignored.

- Tom

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://339121]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-19 02:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found