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

I created a file that upon exit, tracks the exit point, so I know what happened... however, the file runs every 4 minutes. That is 15 times per hour and 360 times per day.

So, I want to track how many entries, I want to keep it at 100 entries, so I can look back for the past 6 and 2/3 hours, that should be good enough... So at the top of the file, I want to have it keep only the entries numbered 2-100, so it should strip out the first line only, so it will now be 1-99 then when it exists it will write line 100, so next time it will delete line 1 again, etc...

I don't know exactly how to do it effeciently though. I think there is a way to count the lines like this:
open(FILE,"/path/to/file.txt") or die "could not open file: $!"; my @_lines = <FILE>; # After I open the file for reading close(FILE); my $_numOfLines = scalar @_lines;# Is that right? I think it is, but m +y mind is rusty, been a while since I learned all of the perl program +ming
So if @_lines had 100 lines, can I not do a delete and have it remove only the first line and then just write the file again?

any ideas how how to do that?

Thank you kindly for any advice.

Richard

Replies are listed 'Best First'.
Re: reading files
by daxim (Curate) on Oct 31, 2012 at 10:14 UTC
Re: reading files
by space_monk (Chaplain) on Oct 31, 2012 at 10:51 UTC

    From what I'm reading, I really think you should be approaching the problem differently. This might be heresy on here, but rather than writing some fancy PERL, maybe you should be letting your system do the work for you?

    What you're describing really sounds as though its something that should be written to the system log and tracked there. Then there are nice friendly tools to rotate your logs and ensure they don't get out of hand :-)

      or you can like, TAIL the log instead of CATing it :) tail -n 100
Re: reading files
by Anonymous Monk on Oct 31, 2012 at 10:17 UTC
    sub chunkit { my $filename = shift; use Tie::File; tie my @array, 'Tie::File', $filename or die "cant chunkit $filename : $!\n$^E "; splice @array, 0, -100; # remove all but last 100 untie @array; }
Re: reading files
by greengaroo (Hermit) on Oct 31, 2012 at 14:45 UTC

    That would be the easiest Pure-Perl way of doing it:

    open(FILE,"/path/to/file.txt") or die "could not open file: $!"; my @_lines = <FILE>; # After I open the file for reading close(FILE); if ( scalar(@_lines) >= 100 ) { shift( @_lines ) for ( 0 .. (scalar(@_lines) - 100) ); # shift removes the first element of the list # but what if you had 110 lines? you have to loop # and you start the loop at 0 because if you have 100 lines # 100 - 100 = 0 and 0 .. 0 will give you one iteration # but 1 .. 0 would not loop at all # then you overwrite the file open(FILE,">/path/to/file.txt") or die "could not open file: $!"; print FILE @_lines; close FILE; }

    There are no stupid questions, but there are a lot of inquisitive idiots.