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.