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

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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://964843]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (1)
As of 2017-08-18 04:31 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (294 votes). Check out past polls.