in reply to managing log files
Based on your described algorithm, I'd suggest using Tie::File. If you're using large log files, be sure to read the section on memory.
Basically, this module allows you to "tie" a file to an array where each line in the file is an element in the array. This allows you to use array control functions (such as push and pop) to manipulate the file (such as adding a new line of text and deleting a line.)
The (untested) code below should help you get started with using Tie::File with what you're trying to do.
use strict; use Tie::File; my $filename = "some.log"; my @file; tie @file, 'Tie::File', $filename || die "Unable to open file\n"; my $index = 0; foreach my $line (@file) { if ($line =~ m/xyz/i) { my $var1 = $file[$index-4]; my $var2 = $file[$index+4]; print "Found values '$var1' and '$var2'\n"; } $index++; } untie @file;
|
|---|