use warnings; use strict;
Check the success of open, use its 3-argument form and use lexical filehandles, $fh:
open my $fh, '<', $File or die "can not open $File: $!"; while (<$fh>) { ... } close $fh;
This:
if ( $_ =~ /([0-9]+\:[0-9]+\:[0-9]+) ([0-9]+) (\w+) (\w+) (\w+)/ ){
is more concisely written as this:
if (/(\d+:\d+:\d+) (\d+) (\w+) (\w+) (\w+)/ ){
Update: OK, I'm back in from shoveling snow. You should choose a better name for your hash data structure than %hash. I dont think you need all those extra variables, just stuff them into your hash right away.
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 amoun +t. 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"; } }
In reply to Re: parsing and get the report
by toolic
in thread parsing and get the report
by perlthirst
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |