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

I am having a problem with my script reading a directory, file by file; if it finds the word arrer in the file, it will send a page to Mike and go onto the next file. As it is now, if the word is in that file more than once, Mike will be paged for every one. I only want Mike to be paged at the most once per file. How would I do this? Below is my code...it works too well! Mike is being paged 4 times for a file that has this arerr in it 4 times and he should only be paged once. I do not know in advance if the word arrer will exist in these log files or not. If it does exist I will not know how many times it will occur. Here is my code!

use File::Copy; use File::Basename; @errorcheck = glob ('*.log'); my $error = "ARERR"; my $errorcount = 0; foreach $logrecord (@errorcheck) { open (LOGFILE, "$logrecord"); while (<LOGFILE>) { my $logline = $_; if ($logline =~ /$error/) { $errorcount++; } } if ($errorcount ne 0) {system ('page Mike');} } print "$errorcount\n"; close (LOGFILE);

Thanks in advance for any assistance you can give me!!

Replies are listed 'Best First'.
Re: A single action from one or more items searched for
by VSarkiss (Monsignor) on Apr 17, 2002 at 19:08 UTC

    Just move your if outside the loop:

    foreach $logrecord (@errorcheck) { open (LOGFILE, "$logrecord"); while (<LOGFILE>) { my $logline = $_; if ($logline =~ /$error/) { $errorcount++; } } } system ('page Mike') if $errorcount; print "$errorcount\n";
    Or did I misunderstand your question?

    By the way, use != instead of ne when comparing numbers. Otherwise negative quantities will surprise you.

Re: A single action from one or more items searched for
by grep (Monsignor) on Apr 17, 2002 at 19:06 UTC

    You're on the right path... you need to read up on last, next and LABELS in perlsyn.

    I am concerned that you are not checking the success of you file opens. I promise it will save you time in the future if you slap an or die 'can not open file' at the end your opens



    grep
    Unix - where you can thrown the manual on the keyboard and get a command
Re: A single action from one or more items searched for
by dsb (Chaplain) on Apr 17, 2002 at 19:28 UTC
    As grep suggested, you should look into the next and last flow controllers. Also, remember that once you find the error string once, you do not need to search through the rest of the file.

    FYI, you need only one other statement in your existing code to accomplish your goal. ;)

    Amel - f.k.a. - kel

A reply falls below the community's threshold of quality. You may see it by logging in.