in reply to Re: Regex question
in thread Regex question

That will also fail if you have two matches within "A" lines of each other
01 keep 02 discard B5 03 discard B4 04 discard B3 05 discard B2 06 discard B1 07 somepat 08 discard A1 09 somepat 10 discard A1 11 discard A2 <- Not dicarded 12 discard A3 <- Not dicarded 13 keep

By the way,
defined( $buffer[5] )
reads better as
@buffer > 5

Replies are listed 'Best First'.
Re^3: Regex question
by davido (Cardinal) on Jan 27, 2009 at 04:49 UTC

    Yes, I mentioned that in my writeup. But the fix is pretty straightforward, and is presented in the code below:

    use strict; use warnings; my @buffer; my $re = qr/10/; while (<DATA>) { if ( $_ =~ $re ) { @buffer = (); my $count = 0; while( defined( my $discard = <DATA> ) and $count++ < 2 ){ if( $discard =~ $re ) { $count = 0; } } } else { push(@buffer, $_); if( @buffer > 5 ) { print shift(@buffer); } } } print @buffer; __DATA__ Line 01 Line 02 Line 03 Line 04 Line 05 Line 06 Line 07 Line 08 Line 09 Line 10 Line 11 Line 12 Line 13 Line 14 Line 15 Line 16 Line 17 Line 18 Line 19 Line 20

    Dave