in reply to Regex perl grep usage string match comparison

In addition to the previous suggestions, you can eliminate the loop over @configLineItems by compiling a regular expression with qr like this:

my $regex = join '|', map {quotemeta} @configLineItems; $regex = qr/$regex/;

(If @configLineItems contains regular expressions, then drop the "map {quotemeta}".)

This is something you can do when you read the configuration file before the loop over the input file, storing $regex for use during the main loop over the input. You can then use it like so: if ($Line[6] =~ $storedRegex)

As was said earlier, as much as we like to help, this isn't a code writing service - please try to implement these suggestions and if you need help doing so please don't hesitate to ask.

Replies are listed 'Best First'.
Re^2: Regex perl grep usage string match comparison
by justinkala (Initiate) on Oct 12, 2014 at 02:51 UTC
    Implemented the code and it works but have some more things to be added
    foreach $inputline (@input_array) { my @inputline = split(/\|/, $inputline); $inputline[8] = "Other Application event"; my $lastColumn = "#>"; push @inputline, $lastColumn; #Check ETYPE and change EOUTCOME if ($inputline[2] eq 'INFO') { $inputline[5] = "INFO"; } elsif ($inputline[2] eq 'ERROR') { $inputline[5] = "ERROR"; } #Check EMSG and create new field next to it foreach $configline (@cfg_array) { my @configline_array = split(/\|/, $configline); shift @configline_array; for $configitem (@configline_array) { if($inputline[6] =~ $configitem) { my $lastColumn = pop @inputline; $inputline[8] = $configline_array[0]; push @inputline, $lastColumn; last; } } } $line = join('|', @inputline); print $output_fh $line, "\n"; }
    if($inputline6 =~ $configitem) { In this line which comparison operator shud be put so that if the string I am searching for is "exception" but the line in the file might contain "MessageException" or "Message.Exception" .So primarily to ignore CASE or it could be part of word. Any help on this??