Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Monk Specs.?

by talexb (Chancellor)
on Jan 16, 2002 at 19:35 UTC ( [id://139219]=note: print w/replies, xml ) Need Help??


in reply to Monk Specs.?

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.

Replies are listed 'Best First'.
Re:x2 Monk Specs.?
by grinder (Bishop) on Jan 16, 2002 at 19:39 UTC
    What does this code do? If the target date is found while looking through the log file, print the remainder of the log file.

    Note quite. As soon as the date changes, your script as it stands will stop printing additional lines to the other logfile (which, according to the initial script, is addressed by the LOGFILE file handle). You are printing the records back into the original logfile!

    I ++ you anyway, because apart from that your advice is faultless.

    --
    g r i n d e r
    print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u';
      Eeyow. I hate when that happens. OK, I change that line to print LOG_FILE <LOG>; My heart was in the right place. :)

      --t. alex

      "Of course, you realize that this means war." -- Bugs Bunny.

Re: Re: Monk Specs.?
by particle (Vicar) on Jan 16, 2002 at 19:43 UTC
    um... your points are valid, but this code is incorrect. *ducks*

    while ( <LOG> ) { if ( /$date/ ) { print <LOG>; } }
    firstly, you're reading from and printing to the same filehandle, probably a typo. it should print LOGFILE;. secondly, it's only printing lines that match the date, instead of printing all lines from the matched date until the end of the file.

    ~Particle

      Hmm .. don't think my code is wrong. The if statement watches for the correct date, and when that date's found, the entire rest of the file is output. My original reply had output going to STDOUT, but I later fixed that so it went to LOG_FILE, as in the original post. The input file handle is LOG.

      --t. alex

      "Of course, you realize that this means war." -- Bugs Bunny.

Re: Re: Monk Specs.?
by jdporter (Paladin) on Nov 05, 2002 at 15:30 UTC
    What was that admonition about sucking immense log files into memory?

    And what does this do?

    print LOG_FILE <LOG>;

    How about:
    print LOG_FILE while <LOG>;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://139219]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-03-29 07:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found