R
Robert Wallace
in my code i'm trying to keep a log file of say 10 lines.
if a line is added and the log file is more than 10 lines, then the last
line is removed before a new line is added.
if the log file is less than 10 lines, then the new line will be added
and no lines will be removed.
here's my first attempt:
#append and clean logfile
my(@logFile);
open (READ, $logFile);
chomp(@logFile=<READ>);
close (READ);
my($size)=@logFile;
if ($size > 10){
unshift @logFile, $search;
pop @logFile;
}
my($logFileWrite)=join "\n", @logFile;
open (WRITE, ">$logFile");
print WRITE $logFileWrite;
close (WRITE);
the problem with unshift/pop here is that if there's only one line it'll
still rotate.
so I added the if statement. but if there are say 19 lines, it'll rotate
the 19 lines instead of trimming off 9.
is there a way perhaps to move 10 items from one array to another
like psudo code:
@arrayClean=@array [item 1], [item 2] ... [item 10]...throw out the
rest;
if a line is added and the log file is more than 10 lines, then the last
line is removed before a new line is added.
if the log file is less than 10 lines, then the new line will be added
and no lines will be removed.
here's my first attempt:
#append and clean logfile
my(@logFile);
open (READ, $logFile);
chomp(@logFile=<READ>);
close (READ);
my($size)=@logFile;
if ($size > 10){
unshift @logFile, $search;
pop @logFile;
}
my($logFileWrite)=join "\n", @logFile;
open (WRITE, ">$logFile");
print WRITE $logFileWrite;
close (WRITE);
the problem with unshift/pop here is that if there's only one line it'll
still rotate.
so I added the if statement. but if there are say 19 lines, it'll rotate
the 19 lines instead of trimming off 9.
is there a way perhaps to move 10 items from one array to another
like psudo code:
@arrayClean=@array [item 1], [item 2] ... [item 10]...throw out the
rest;