Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Answer: How do I split a file into separate sections?

by chromatic (Archbishop)
on Apr 03, 2000 at 18:17 UTC ( #6723=categorized answer: print w/replies, xml ) Need Help??

Q&A > regular expressions > How do I split a file into separate sections? - Answer contributed by chromatic

while ($data =~ s!(TI|AU|JN):(.*)?!!s) { push @{$sections{$1}}, $2; }
That will build a hash of arrays with the headers as keys. Things you might want to change are as follows:
  • With the /s modifier on the regex, . will match newlines. You may not want this (but it makes the regex much more complicated)
  • You may not want a hash of arrays. Remove the @ and use the .= operator
  • You may want to match a newline after the colon.
This all depends on your data. Personally, I'd use split in a heartbeat.

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2017-11-22 04:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:













    Results (315 votes). Check out past polls.

    Notices?