open my $fh, '<', $File or die "can not open $File: $!"; while (<$fh>) { ... } close $fh; #### if ( $_ =~ /([0-9]+\:[0-9]+\:[0-9]+) ([0-9]+) (\w+) (\w+) (\w+)/ ){ #### if (/(\d+:\d+:\d+) (\d+) (\w+) (\w+) (\w+)/ ){ #### use strict; use warnings; my %hash; my $File = "input.txt"; open my $fh, '<', $File or die "can not open $File: $!"; while (<$fh>) { chomp; ## taking time, transaction id, status, transaction name and amount. if (/(\d+:\d+:\d+) (\d+) (\w+) (\w+) (\w+)/ ){ $hash{$2} = [($3, $1, $4, $5)]; } ## If the status is stop.. elsif (/(\d+:\d+:\d+) (\d+) (\w)/ ) { $hash{$2}->[0] = $3; my $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. delete $hash{$2}; } } close $fh; print "============================================\n"; ## to print the transaction started but not yet stopped for my $id ( keys %hash ) { if ( $hash{$id}->[0] =~ /start/i ) { print "Transaction with id $id is started but not yet stopped\n"; } }