in reply to processing timelog

Hi sandy105,

you might want to try this:

use strict; use warnings; my ($tot1, $tot2); while (<DATA>) { chomp; s/\s+$//g; next unless /TP Service Request$/ or /P5 Move request$/; my ($datetime, $ms, $inid, $message) = split /,/, $_; my $date = substr $datetime,11; my ($hours, $min, $sec) = map { $_ * 1000} split /:/, $date; $hours *= 60 * 60; $min *= 60; my $total_time = $hours + $min + $sec + $ms; $tot1 = $total_time if $message eq "TP Service Request"; $tot2 = $total_time if $message eq "P5 Move request"; next unless defined $tot2; print "Duration is: ", $tot2 - $tot1, " ms.\n"; last; } __DATA__ 2014-05-29 10:22:21,880,165ab6a8-e736-11e3-8748-8d365226be24,TP Servic +e Request 2014-05-29 10:22:21,962,165ab6a8-e736-11e3-8748-8d365226be24,ProcessNa +me: TC ... (rest of data omitted from this post for brevity)
Executing it gives the following:
$ perl log_files.pl Duration is: 46426 ms.
There are some issues left. For example, this does not work correctly if the date changes between the two dates/times. Please note that I have also tried to make the code more "DRY" (Don't Repeat Yourself).

Replies are listed 'Best First'.
Re^2: processing timelog
by sandy105 (Scribe) on Aug 24, 2014 at 11:29 UTC

    the total time is almost figured out ; i cant seem to think of a way to get process time without writing inefficient & repetitive code :(

      the total time is almost figured out ; i cant seem to think of a way to get process time without writing inefficient & repetitive code :(

      Congratulations, you are about to start programming, start naming subroutines, subroutines that take arguments and return values, and die on critical errors (like can't open file), subroutines with meaningful names that make sense in the context of your application, subroutines whose names are part of your application vocabulary/glossary

      perlintro#Writing subroutines and http://learn.perl.org/books/beginning-perl/ Chapter 8: Subroutines

        i know and i have used subroutines , and i am writing subroutines for converting datetime to proper time , but cant figure out how to add up the process time

        right now i am calculating total time separately and individual times by appending each process time to an hash with id as key and then consolidating ..i just cant figure out a elegant way that's it.thats why asked for help .any ideas or pseudocode would help

      Consider using a database perhaps ?
      #!perl use strict; use warnings; use DBI; #create db my $dbfile = 'database.sqlite'; unlink($dbfile); my $dbh = DBI->connect('dbi:SQLite:dbname='.$dbfile , undef , undef , {RaiseError =>1, AutoCommit =>0}) or die $DBI::errstr; $dbh->do('CREATE TABLE DATA (F1 datetime,F2 INTEGER,F3,F4)'); $dbh->do('CREATE TABLE STARTED (F1 datetime,F2,F3)'); $dbh->commit; # load data my $sth = $dbh->prepare('INSERT INTO DATA VALUES (?,?,?,?)'); while (<DATA>){ chomp; my @f = split ','; if (@f==4){ $sth->execute(@f); } else { print "Input Line $. Skipped : $_\n"; } } $dbh->commit; # select started records $dbh->do("INSERT INTO STARTED SELECT F1,F2,F3 FROM DATA WHERE F4 LIKE '%started%'"); $dbh->commit; # calculate duration from start my $ar = $dbh->selectall_arrayref(" SELECT D.F1,D.F2,D.F3,D.F4, (1000*strftime('%s',D.F1))+D.F2 - (1000*strftime('%s',S.F1))-S.F2 FROM DATA AS D LEFT JOIN STARTED AS S ON D.F3 = S.F3"); for (@$ar){ printf "@$_\n"; } $dbh->disconnect; __DATA__
      poj
Re^2: processing timelog
by sandy105 (Scribe) on Aug 25, 2014 at 08:11 UTC
    thanks this did help me,and i learned a better approach .this is certainly very "dry" :)