1. Use a regular expression to get only the bits of localtime you are after
    my ($current_date) = map { qr/$_/ } (scalar localtime) =~ /\w+ +(\w+ + +\d+)/;
  2. Rather than put each error in separate quotes I'd rather use qw().
    my @errors = map {qr/$_/} qw( error4 error3 error2 error1 );
    and put them in a meaningful variable.
  3. Since these variables are only used in a regex we might as well precompile them (the maps).
  4. It is good you test that you test that you opened a file, it is better to say why you couldn't
    open my $LOGFILE, '<', $log_file or die "can not open $log_file for writing: $!\n";
  5. Declare variables in as small as scope as possible, that is loop variable get declared as part of the loop, not left floating around. Replace
    $w = 1; while ($w <= @word_search) { # uses $w - 1 to get $word_search[$w - 1] $w++; }
    with
    foreach my $w ( 0 .. $#word_search ) { # uses $w to get $word_search[$w] }
    or better still don't split the line and do away with the loop altogether.
  6. This is where your logic utterly fails. You test the same word for equality with two different words, $pattern_date and "error4". Not only that but the date from the log file is in two separate words since split them apart to get your list of words.

    if ($word_search[$w-1] eq $pattern_date && $word_search[$w-1] eq $arra +y[$count])
    Note you don't initialise $count or ever change its value, you are always testing against the same error.

  7. Use regular expressions to test for the presence of the date and the errors, it is easier than splitting a line into words and iterating over the list.
    if ( $line =~ /$current_date/ ) { foreach my $error (@errors) { # count all the errors on the line ++$error_count while ($line =~ /($error)/g); } }

  8. Use perltidy to format your code. It will be much easier to read.

I leave final assembly to the reader ;-).

Just noticed I had clicked on create rather than preview while composing this. Hope I didn't cause too much distress to our regular viewers.


In reply to Re^3: search log file with multiple word and count number of success by hipowls
in thread search log file with multiple word and count number of success by steadybompipi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.