mantra2006 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks

I am trying to write a program to read a file and findout in a line
certain words are there or not and if there then return true else return false
. For eg. if the line has warning or
connection refused or failed then program should
return true otherwise false. I struck with comparing for words...
# Check the log file to determine scp success or failure open(MYLOG, "$log_file") or die "Can't open the log file $log_file"; @raw_data=<MYLOG>; close MYLOG; foreach $line (@raw_data){ print $line; # I am struck for logic comparing words... }
Thanks & Regards
Sridhar

Replies are listed 'Best First'.
Re: Reading lines and matching words
by Old_Gray_Bear (Bishop) on Apr 11, 2007 at 20:09 UTC
    As a hint -- look up the index function and take a look at the chapter on 'Regular Expressions' in the Camel (Programming Perl). Write back when you have some more code for us to look at.

    ----
    I Go Back to Sleep, Now.

    OGB

Re: Reading lines and matching words
by duff (Parson) on Apr 11, 2007 at 20:09 UTC

    Is there some reason you aren't using grep(1)?

Re: Reading lines and matching words
by RobPayne (Chaplain) on Apr 11, 2007 at 20:11 UTC
    You didn't specify exactly what type of matching you need, here's a very simple regex pattern match to return 1 if the line contains any one of three words. Otherwise, it returns 0.
    if ($line =~ /word1|word2|word3/) { return 1; } else { return 0; }
    Your code didn't show a call to a subroutine, so I'm assuming you *really* meant return. As duff said, this is what grep does.
      Thank you Rob
      It worked!!!
      Thanks & Regards
      Sridhar