in reply to Re: Printing line before matching expression
in thread Printing line before matching expression
It seems a bit of overhead to me if you use an array of the required buffer size to store the linked list and the data. What you save compared to other proposals is the pushing and shifting which involves a lot of copying data around. This can be done more easily by cycling through the buffer using the modulo operator on the line number of the data.
use strict; use warnings; my $lookback = 3; my $match = qr/\wiz/; my @lbuff; while (<DATA>) { /$match/ and defined $lbuff[ $. % $lookback ] and print "$lbuff[ $ +. % $lookback ]"; $lbuff[ $. % $lookback ] = $_; } __DATA__ foo bar baz biz buz goo car caz ciz cuz
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: Printing line before matching expression
by Anonymous Monk on Sep 11, 2013 at 07:54 UTC | |
by hdb (Monsignor) on Sep 11, 2013 at 08:21 UTC | |
Re^3: Printing line before matching expression
by jaredor (Priest) on Sep 11, 2013 at 18:56 UTC |