Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
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 avoiding work at the Monastery: (7)
As of 2014-12-22 11:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (116 votes), past polls