in reply to Re: Extract ranges of lines from a file, saving each range to a separate file
in thread Extract ranges of lines from a file, saving each range to a separate file
Just FYI, FWIW, the .. operator has a couple of features that can replace the duplicated regex matching.
First, the value of .. isn't just FALSE or TRUE, it's also a line number relative to the start of the range. Before the start, the value is 0 (aka FALSE). When the start of the range is matched, the value is 1. this number increments until the end of the range. So, you can:
my $rln = /$start_re/ .. /$end_re/; if $rln == 1 { # open output file next; } if $rln > 1 { print $out_fh $_; }
Second, when the range ends, the number has 'E0' appended. So, you can:
if rindex($rln, 'E0') { close $out_fh; next; }
rindex is a simple string search that works backwards, so has much less overhead than another regex match. And appending 'E0' to a string of digits is still a valid number - numerically equal to the number without the 'E0'.
|
|---|