Note that there are some explanatory texts on larger screens.

plurals
  1. POPattern matching with filehandlers and regular expressions in Perl
    primarykey
    data
    text
    <p>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:</p> <pre><code>foreach $element (@fr_array) { open (LOGFILE, "&lt;", $logname) or die $!; while (&lt;LOGFILE&gt;) { #While reading one line at a time from the log.... chomp; $log_string = &lt;LOGFILE&gt;; if ($log_string =~ /\+$element\+/) { $fr_count++; print "Counter increment since $log_string is the same as $element."; $log_match++; } } </code></pre> <p><em>EDIT</em></p> <p>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.</p> <pre><code>open (LOGFILE, "&lt;", $logname) or die $!; while($log_string = &lt;LOGFILE&gt;) { chomp $log_string; foreach $element (@fr_array) { $fr_count++ if (grep {"Action=123 Cls=AM"} $log_string) </code></pre> <p>Thanks again, all, for the assistance and for the patience. :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload