in reply to Comparing Dates and Reoccurance - Part II

My previous response indicated why you were not getting output.

Anyway, at this point, since I'm whoring for XP to make parson, here is a working solution: (Change the input file name).

#!/usr/local/bin/perl -w use strict; use warnings; use Time::Local; my $infile = 'test-logdata.txt'; my $outfile = 'output.2008-01-01.log'; my($fh_out, $fh); open($fh_out, '>', $outfile) or die "Could not open outfile: $!"; open($fh, '<', $infile) or die "Could not open logfile: $!"; my %track; my $header=<<HEADER; TIDS time Occurance ==================================================== HEADER print $fh_out $header; while (<$fh>){ my ($date,$channel,$id) = /^(\S+\s\S+).+channel = (\w+).+tids = (\w+ +)/; my $time = dateconv($date); my $prevtime = $track{$id}{TIME}; $track{$id}{TIME}=$time; $track{$id}{DATE}=$date; if ($prevtime and $time - $prevtime > 3600){ $track{$id}{COUNT}++; print $fh_out "$id\t$date\t$track{$id}{COUNT}\n" ; } } sub dateconv{ my $d = shift; my %month = qw[jan 1 feb 2 mar 3 apr 4 may 5 jun 6 jul 7 aug 8 sep 9 oct 10 nov 11 dec 12]; my @p = $d=~/(\d+)-(\w+)-(\d+)\s(\d+):(\d+):(\d+)/; $p[1]=$month{ lc $p[1] } - 1; return timelocal(@p[5,4,3,2,1,0]); #timelocal($sec,$min,$hour,$mday,$mon,$year); } close $fh_out; close $fh;

     "As you get older three things happen. The first is your memory goes, and I can't remember the other two... " - Sir Norman Wisdom

Replies are listed 'Best First'.
Re^2: Comparing Dates and Reoccurance - Part II
by tuakilan (Acolyte) on Mar 13, 2008 at 03:46 UTC
    good day netwallah,

    appreciate much of your help here ....

    but it produce no result other than the header that reads the following :

    IDS TIME OCCURANCES ===================================================

    am I missing something here ?

    #!/usr/local/bin/perl -w use strict; use warnings; use Time::Local; my $infile = 'input.2008-01-01.log'; my $outfile = 'output.2008-01-01.log'; my $channel = 'seven'; my($fh_out, $fh); open($fh, '<', $infile) or die "Could not open logfile: $!"; open($fh_out, '>', $outfile) or die "Could not open outfile: $!"; my %track; while (<$fh>){ next unless /$channel/; my ($date,$channel,$id) = /^(\S+\s\S+).+channel = (\w+).+id = (\w+)/ +; my $time = dateconv($id); my $prevtime = $track{$id}{TIME}; $track{$id}{TIME}=$time; $track{$id}{DATE}=$date; if ($prevtime and $time - $prevtime > 3600){ $track{$id}{COUNT}++; print $fh_out "$id\t$date\t$track{$id}{COUNT}\n" ; } } sub dateconv{ my $d = shift; my %month = qw[jan 1 feb 2 mar 3 apr 4 may 5 jun 6 jul 7 aug 8 sep 9 oct 10 nov 11 dec 12]; my @p = $d=~/(\d+)-(\w+)-(\d+)\s(\d+):(\d+):(\d+)/; $p[1]=$month{ lc $p[1] } - 1; return timelocal(@p[5,4,3,2,1,0]); #timelocal($sec,$min,$hour,$mday,$mon,$year); } my $header=<<HEADER; ID TIME OCCURANCES ===================================================== HEADER print $fh_out $header; close $fh_out; close $fh;
      Yes, you are missing something ;-)

      You added a line to my code:

      next unless /$channel/;
      The problem is that line is in the wrong place, and probably does not do what you think it is doing.

      You are checking the value of $channel before that value is set as intended by my original code. You have also declared $channel twice.

      The other line you mangled now reads

      my $time = dateconv($id);
      Please try to understand the code, before you choose to modify it.

      Now that I have made parson, I'm less motivated to spoon-feed beyond this point.

      Update: Use mr_mischief's(++) excellent update to my code.

           "As you get older three things happen. The first is your memory goes, and I can't remember the other two... " - Sir Norman Wisdom