In terms of a code review, I can give you the following suggestions and comments.
- You're reading in the entire log file into an array with the line my @input = <LOG>; I don't know about you, but the log files I come across vary between large and immense. There's no reason to suck the entire file into memory. Better just to read a line at a time.
- You are apprently interested in logging where in the file a particular date starts. If you'd used while(<LOG>) you could have just used the builtin variable $.. In anycase, why not use something meaningful like $LineNumber rather than the anonymous $counter.
- Your declaration of $_ is redundant. Perl assumes $_ if you don't declare a loop variables. But that line disappears if you use the while loop mentioned above.
- At the end you test if the log file is 'blank'; wouldn't it be better to explain that you're testing to see if the date you were looking for was found? In any case, you've used eq which is a string compare. To see if a value is the same as zero, use ==
So if I were rewriting the code, I'd say
while ( <LOG> )
{
if ( /$date/ ) { print LOG_FILE <LOG>; } # UPDATED
}
What does this code do? If the target date is found while looking through the log file, print the remainder of the log file. The print will continue until the file runs out. At that point the loop will continue, but the log file has run out, and the loop will exit.
(This assumes that the data is sorted!)
Study hard. :)
--t. alex
"Of course, you realize that this means war." -- Bugs Bunny.
Update: Added LOG_FILE file handle that was missing from the original post, as indicated by replies after this note.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.