Actually, I see a couple problems with the logic in your script. First (a minor point), you should call "time()" and compute the comparison value just once, before going into the loop, and store these a scalars.

Second, if you restructure the logic a little bit, you can do it all with just one while loop -- something like this:

my $ref_time = time(); my $ref_delta = $serverRef->{ScanErrlogLastDays}*24*3600; while (<LOG>) { next unless ( /^\s*([\d\/\-]+\s+[\d\.\:]+)\s+/ ); next if ( $ref_time - str2time( $1 ) > $ref_delta ); $ref->{start_check_datetime} = $1; # put all the stuff from the second while loop here } # all done, and you didn't miss the first record
(update: fixed the second condition (from "unless" to "if") to match the OP's intent. Actually, fixed two other typos in that same line as well -- not having a way to test my work makes me less reliable!)

another update: Since consecutive lines in the input log file are guaranteed to be in chronological order, it's probably worthwhile to avoid doing the time arithmetic over and over, once you hit the first sought-for record -- I realize that this must have been (part of) the reasoning behind having two while loops in your approach. I also realize that my initial suggestion would keep assigning new values to ref->{start_check_datetime} -- which is probably a mistake. You could fix this as follows:

my $seeking = 1; my $ref_time = time(); my $ref_delta = $serverRef->{ScanErrlogLastDays}*24*3600; while (<LOG>) { if ( $seeking ) { next unless ( /^\s*([\d\/\-]+\s+[\d\.\:]+)\s+/ ); next if ( $ref_time - str2time( $1 ) > $ref_delta ); $ref->{start_check_datetime} = $1; $seeking = 0; } # put all the stuff from the second while loop here }

In reply to Re: Skip and Scan Lines by graff
in thread Skip and Scan Lines by jc7

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.