$File = "input.txt"; open(FH,$File); while ( ) { ## Chomp the read line. i.e removing \n available at the end chomp; ## taking time, transaction id, status, transaction name and amount. if ( $_ =~ /([0-9]+\:[0-9]+\:[0-9]+) ([0-9]+) (\w+) (\w+) (\w+)/ ){ $time = $1; $tid = $2; $status = $3; $trname = $4; $tramount = $5; $hash{$2}->[0] = $status; $hash{$2}->[1] = $time; $hash{$2}->[2] = $trname; $hash{$2}->[3] = $tramount; } ## If the status is stop.. elsif ( $_ =~ /([0-9]+\:[0-9]+\:[0-9]+) ([0-9]+) (\w)/ ) { $status = $3; $hash{$2}->[0] = $status; $difftime = find_diff($hash{$2}->[1],$1); $hash{$2}->[1] = $difftime; print "Transaction id $2 name ".$hash{$2}->[2]." Amount ". $hash{$2}->[3]." Duration ".$hash{$2}->[1]."\n"; # If the transaction is stopped initialising the values. $hash{$2}->[0] = 0; $hash{$2}->[1] = 0; $hash{$2}->[2] = 0; $hash{$2}->[3] = 0; } } print "============================================\n"; ## to print the transaction started but not yet stopped foreach $id ( keys %hash ) { if ( $hash{$id}->[0] =~ /start/i ) { print "Transaction with id $id is started but not yet stopped\n"; } } sub find_diff { ## Get the start time and end time and store that in a variable. my ( $begin, $end ) = @_; # Declaration of local variables. my ( $hour , $min, $sec , $hour1, $min1, $sec1, $tdiff); # split the start time which has a : as a seperator, and store hour, min # sec. ($hour, $min, $sec ) = split(/\:/,$begin); # Convert the start time into seconds. $stime = ( $hour *3600 ) + ($min *60) + $sec; # split the end time which has a : as a seperator, and store hour, min # sec. ($hour1, $min1, $sec1 ) = split(/\:/,$end); # Convert the end time into seconds. $etime = ( $hour1 *3600 ) + ($min1 *60) + $sec1; # subtract the end time from start time. $diff = $etime - $stime; # conversion of seconds to hour, min and sec. $hour = int($diff/3600); $min1 = int($diff%3600); $min = int($min1/60); $sec = int($min1%60);# Form the hour min sec like hh:mm:ss $tdiff = $hour.":".$min.":".$sec; # return the output. return $tdiff; }