in reply to comb a logfile for time diff and occourance

And last time you showed me some code;-)

Are you just interested in counting the number of times that TWO occurs in each hour? Just an outline.

my %stats; while ( my $line = <$file> ) { next unless $line =~ /TWO/; my ($time) = $line =~ /^(\d\d\d\d-\w\w\w-\d\d \d\d)/)/; ++$stats{$time}; }

Update: Put $time in () and changed ++$stats{$date} to ++$stats{$time}. Thanks to CountZero for pointing out the error.

Replies are listed 'Best First'.
Re^2: comb a logfile for time diff and occourance
by CountZero (Bishop) on Feb 18, 2008 at 10:51 UTC
    That won't work: the time difference must be more than one hour.

    Also you use $date as a key to your hash, but you never put anything in it. You probably meant $time.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      Thanks for catching the typo, I've corrected the original post.

      You are right to some extent, I'd missed the bit about identical refs. However the specs aren't clear and it is not obvious what the OP really wants to report.

      A modification of the code to

      while ( my $line = <$file> ) { next unless $line =~ /TWO/; my ($time, $ref) = $line =~ /^(\d\d\d\d-\w\w\w-\d\d \d\d)/).*refs += (\d+)$/; ++$stats{$time}{$ref}; }
      may suffice. It will keep a count of how many times each ref number occurs during each hour. Typically data is reported over fixed intervals of time and this approach certainly has the advantage of simplicity.

        "Lack of clear specifications" is probably the worst part of having to implement something for someone else. Rarely they have a clear view of what they want, but that of course will not stop them from changing their targets and blaming the programmers if projects overrun their allowed budgets of time and money.

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re^2: comb a logfile for time diff and occourance
by Anonymous Monk on Feb 20, 2008 at 03:13 UTC
    hi hipowls, i pasted your codes into the following
    my $in_file = "$logfile"; open my $in_fh, '<', $in_file or die "Could not open file $in_file: $! ++"; my $out_file = 'output.txt'; open my $out_fh, '>', $out_file or die "Could not open file $out_file: ++ $!"; while ( my $line = <$in_fh> ) { print {$out_fh} $line if $line =~ /production/; } my %stats; while ( my $line = <$file> ) { next unless $line =~ /TWO/; my ($time) = $line =~ /^(\d\d\d\d-\w\w\w-\d\d \d\d)/)/; ++$stats{$time}; } close $in_fh or die "Could not close file $in_file: $!"; close $out_fh or die "Could not close file $out_file: $!";
    but i get error in EnginSite Perl Editor LE
    C:\code>perl -wc a.pl syntax error at a.pl line 37, near "/^(\d\d\d\d-\w\w\w-\d\d \d\d)/)" a.pl had compilation errors.
    Any pointers ?