Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: grab 2 blocks of texts from file

by graff (Chancellor)
on Apr 13, 2012 at 00:16 UTC ( #964843=note: print w/ replies, xml ) Need Help??


in reply to grab 2 blocks of texts from file

Since you seem to be slurping the entire content of the data file into a scalar variable, it might be easier just to use regex matches on that one big string:

if ( $filedata =~ /#begin\n (.*?) #end\n .*? #begin2\n (.*?) #end2/sx +) { ( $block1, $block2 ) = ( $1, $2 ); @arr_block1 = split /\n/, $block1; @arr_block2 = split /\n/, $block2; }
Recall from your studies of perlre that the "s" modifier allows the "." wildcard to match new-lines as well as all other characters, and "x" allows for using spaces in the regex for legibility (not for matching literal spaces).

I chose to match both blocks with one regex, but you could do each block as a separate regex match if you want.


Comment on Re: grab 2 blocks of texts from file
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (17)
As of 2015-07-28 14:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (255 votes), past polls