Pattern matching with filehandlers and regular expressions in Perl

Following up from an earlier question I asked about, I'm trying to read data from two files then parse line by line through one file, looking to match the string contained on each line of the other file. The first is a log file, and the second I will call my key file. I decided to iterate through the key file using a foreach loop then embed inside of it, a while loop that will step through each line of the log. I'm trying to use pattern matching to identify a particular string, but this string is either being surrounded by two plus signs (i.e. +name+) or a plus and an asterisk (i.e. +name*). Unfortunately I'm not having much luck getting the pattern matching to work....I've included the body of my pattern searching code segment below:

foreach $element (@fr_array) {
open (LOGFILE, "<", $logname)  or die $!;
while (<LOGFILE>) { #While reading one line at a time from the log....
  chomp;
  $log_string = <LOGFILE>;
  if ($log_string =~ /\+$element\+/) {
     $fr_count++;
     print "Counter increment since $log_string is the same as $element.";
     $log_match++;
     }
  }

EDIT

Thanks for the feedback, everyone...a couple of the commands I invoked were used to help me trace my way through the program and locate the problem....I took some advice and after a bit more research, am trying to use the grep command instead, but the pattern matching still doesn't seem to be working right. It seems to be resolving as true no matter what I use as the search string. I've been looking over the code all day but I'm not quite sure where to go from here, either. After a failed success with the regular expressions, I decided to go with a different search string to use, this one composed of all literals except for a couple of equals signs. Now, just to clarify, I'm not interested in doing anything with the matches once they're located, I just need to keep track of how many matches there are, hence my use of a counter. Anyway, I'm including an updated code segment of where I am now. I've confirmed that my loops are working correctly, it's just the pattern matching that's the problem. I am getting results now, but the number is not accurate....my test log should only return 1 or 2 but it's spitting out 204 at me.

open (LOGFILE, "<", $logname)  or die $!;
while($log_string = <LOGFILE>) {
   chomp $log_string;
   foreach $element (@fr_array) {
      $fr_count++ if (grep {"Action=123 Cls=AM"} $log_string)

Thanks again, all, for the assistance and for the patience. :)

Answers


As I pointed out in the comment, you are trying to simulate 'grep -f key_file log_file' command in perl. See if this grep command can help you.

For this perl code you are trying to open the file for each element in @fr_array. It is not needed and makes code inefficient. Moreover you are reading LOGFILE twice in the while loop which needs to be corrected. A example code can be read like below. Note that + or * can be matched by [\+\*] in regex.

open (LOGFILE, "<", $logname)  or die $!;
while(my $log_string = <LOGFILE>) {
  chomp $log_string;
  foreach $element (@fr_array) {
    if ($log_string =~ /\+$element[\+\*]/) {
      $fr_count++;
      print "Counter increment since $log_string is the same as $element.";
      $log_match++;
    }
  }
}

Need Your Help

Passing Form OpenArgs errors as NULL?

vba access-vba

I am trying to open a form as a dialog and also pass a string in the OpenArgs property. No matter what I try, I get an Invalid use of null error.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.