in reply to Not able to capture information

This modification to your code:

use strict; use warnings; while(<DATA>) { chomp($_); if ($_ =~ m/\[(\d{4}\/\d{2}\/\d{2}\s+\d{2}\:\d{2}\:\d{2})\]\s+\[(\ +d{1,3})\]\s+ERRORMSG\s+(.*)/) { my $date = $1; my $err_no = $2; my $err_msg = $3; if ($. > 1) { print qq{\n}; } print "$date === $err_no === $err_msg"; } else { print qq{ $_}; } } print qq{\n};

produces this output:

ken@ganymede: ~/tmp $ pm_multiline_regex.pl 2012/02/16 00:08:34 === 29 === unknown error Can't insert into price +table Please check Valueprice.pm line 52. 2012/02/16 00:08:34 === 39 === Invalid User 2012/02/16 00:14:52 === 105 === missing conversion rate 2012/02/16 00:14:52 === 29 === Can't use an undefined value as a HASH + reference at Value.pm line 77. ken@ganymede: ~/tmp $

-- Ken

Replies are listed 'Best First'.
Re^2: Not able to capture information
by Don Coyote (Hermit) on Feb 17, 2012 at 10:10 UTC

    I could see this required an if else statement but not how to prevent requiring data storage during the subroutine, that is, using arrays (see my comment below). So simple control of the \n character at the start of a line before flow control kicks in can save a lot of cpu. Rather than at the end where you need to hold the line while the data is fed in to find out if the next line is a match or not.

    Drat, I was just starting to enjoy my array solutions.

      Given he probably wants to print to a log file, you could remove the data storage overhead by changing

      my @linearr; ... print @linearr;

      to

      use Tie::File; tie my @linearr, 'Tie::File', 'noa.log' or die $!; ... untie @linearr;

      You still might want to tweak the internals of the loop.

      -- Ken