in reply to Efficient log parsing?

Add the "c" modifier, if you wan an array-extraction without position reset:
"If you don't want the position reset after failure to match, add the //c, as in /regexp/gc."
So your code should read:
while (@x = /($regex)/gc) { push @buffer, [ @x ]; # Or, if you localize (my) @x, push @buffer, +\@x ; }

     "As you get older three things happen. The first is your memory goes, and I can't remember the other two... " - Sir Norman Wisdom

Replies are listed 'Best First'.
Re^2: Efficient log parsing?
by zrajm (Beadle) on Dec 14, 2007 at 07:01 UTC
    No. That won't work. The loop will only be executed once because of the /g flag, which causes all matches to be returned at once.

    Sorry. :(

      Good catch.

      OK - here is a somewhat contrived variation that seems to work for me:

      use strict; $_="this that other thing that thw tests sometimes"; my $regex = qr/(t..)\w+\s+?(\w\w)(.+)/; my @buffer; my @x; while ( (@x[0..1],$_) = /$regex/) { push @buffer, [ @x ]; print join(",",@x) . ";\n"; }
      Changes:
      • You need to know how many elements to expect
      • The regex has been modified to return all remaining information as the final element
      • The final element gets re-stored into $_
      • No more /g modifier

           "As you get older three things happen. The first is your memory goes, and I can't remember the other two... " - Sir Norman Wisdom