On 11/19/10 Fri Nov 19, 2010 11:25 AM, "Al Oomens" <aloom...@gmail.com> scribbled:
> I am trying to write a script that will find the start and end times > for specific transactions, and calculate the elapsed time for each > transaction. I am looking for the "Start", grabbing the transaction > number, then looking for the matching "End", which works fine most of > the time. The problem I am having is that the transactions do not > always finish before the next one begins, so specific transactions may > have their start and end times interspersed. > > A simplified example would be: > > 201011191101 - T122 - Start > ... > 201011191115 - T122 - End > ... > 201011191118 - T123 - Start > .... > 201011191127 - T124 - Start > .... > 201011191129 - T124 - End > .... > 201011191130 - T123 - End You need to save all of the Start records in a data structure. When the corresponding End record is read, fetch the Start record, compute the transaction time, and print or otherwise process the result. In Perl, since you have unique identifiers for each transaction, the best way is to use a hash with the ID as key and the Start time as value: Define the hash: my %start_time; After reading and parsing a Start record: $start_time{$id} = $time; After reading and parsing an End record: if( exists $start_time($id} ) { my $transaction_duration = $time - $start_time{$id}; # print or save the transaction data }else{ # no start time found for end :( } > > How would you go about this in Perl? Also, it is possible that there > might be a Start in one log file, and the matching End would be in the > next log file. How do you account for those situations. Just read the files in chronological order and don't reset the %start_time hash between files. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/