in reply to Re^2: Extracting blocks of text
in thread Extracting blocks of text
Assuming the file is small enough to slurp, then split does the job nicely:
#! perl -slw use strict; my @array = split 'term', do{ local $/; <DATA> }; shift @array; ## Discard leading null print '---', "\n", $_, "\n" for @array; __DATA__ term { yada yada 12345 () ... } term only occurs here { could be 30 lines here but never that word again until another block starts yadada } term, etc.
That discards the term itself. If you want to retain the term in each element, then perhaps the simplest way is to just put it back after the split. Just substitute this line into the above.
my @array = map{ "term$_" } split 'term', do{ local $/; <DATA> };
|
---|