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

Hello wise monks,

I have a log file that I need to read and parse which would normally be easy enough, however each log entry is 3 lines long. I am not sure whether I should be reading either 3 lines at a time or come up with something to figure out what line it is and then choose how to deal with it.

I am thinking that reading it in chunks of 3 lines would be best, but I don't have a clue where to look for examples or how to implement it myself. Any Help is appreciated :) PLease Disregard, I found a solution in another thread that I had previously missed

Replies are listed 'Best First'.
Re: Question about reading a file...
by BioLion (Curate) on Nov 04, 2009 at 21:29 UTC

    Maybe a simple approach?

    use strict; use warnings; my @log = (); while (<DATA>){ chomp; push @log, $_; if (scalar@log ==3){ do_stuff(\@log); @log = (); } } sub do_stuff{ my $ref = shift; print join ":", @$ref, "\n"; } __DATA__ 1 2 3 4 5 6

    Gives :

    1:2:3: 4:5:6:

    Argh. Only just seen your update... what solution did you go for in the end?

    Just a something something...
      :) yours :P
Re: Question about reading a file...
by gmargo (Hermit) on Nov 04, 2009 at 22:21 UTC

    Sorry for commenting after you already found a solution. I too am curious as to how you solved it.

    I like BioLion's solution, except I would add regular expressions to definitively identify which of the 3 log lines you have. In other words, make sure that you are grouping the correct 3 lines together.

    Since this is a log file, and log files are commonly rotated and/or truncated, you cannot be sure that the first line in your log file is the first line in a 3-line group. It is safer to not make that assumption.

      Well, there is more work to do here, as I have run into the problem where I am not alwyas grabbing the correct 3 lines. I will post a solution once I get it complete...I have a few ideas, I will just have to delve into my perl books to see how to implement that bit I think...hmmmmmmm