You can use the range operator, which acts as a trigger in a scalar context:
foreach ( @parseme ) { if ( /Begin bangle tracking log/ .. /End bangle tracking log/ ) { push @array, $_; }
I used $_ for the foreach because it allows for a more concise syntax. You can use another variable if you want, but then you have to write a condition something like:
if ( $line =~ /Begin .../ .. $line =~ /End .../ ) {
which may be more readable in some additional parentheses:
if ( ($line =~ /Begin .../) .. ($line =~ /End .../) ) {
One problem with the trigger operator is that it remembers its state even after the loop ends. This means that if you intend to start the loop again, you really need to make sure that the @parseme array ends with the line corresponding to the line /End .../ regexp, so that the trigger will be in a known state the next time the loop starts.
Edit:. If you want to process the collected lines as soon as you reach the footer line, you can do this by specifying the return value of the statement .. which ends with E0 on the last line:
foreach ( @parseme ) { my $in_block = /Begin bangle tracking log/ .. /End bangle tracking log/; if ( $in_block ) { push @array, $_; } if ( $in_block =~ /E0$/ ) {
source share