Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Help Parsing a Text File

by tcf22 (Priest)
on Mar 23, 2004 at 19:02 UTC ( #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
Node Status?
node history
Node Type: note [id://339121]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2020-05-27 23:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (162 votes). Check out past polls.

    Notices?