more useful options | |
PerlMonks |
Re^2: grab 'n' lines from a file above and below a /match/by Aristotle (Chancellor) |
on Sep 17, 2004 at 06:23 UTC ( [id://391683]=note: print w/replies, xml ) | Need Help?? |
It's actually slower and more memory intensive than any of the other solutions. Tie::File internally keeps a list of byte offsets for all the lines, and it needs lot of additional overhead that is supposed to optimize writes which you never make any use of. Your code also doesn't get the edge cases right: if there's a match within less than $context lines of the previous, it will be missed. You gave me an idea with regards to memory consumption, though:
This only needs to keep $context offsets in memory. Update: fixed bugs. It was ( 0 ) x $context which gave one too few lines of before-context and $here - $context_start + length which of course ate too much input — but that wasn't obvious with my test data. Oopsie. Makeshifts last the longest.
In Section
Seekers of Perl Wisdom
|
|