my %SMPP_count; foreach my $file (@files) { open (FILE,"$file"); while() { next unless /\b(?:GSM|SMPP)\b/; # avoid uninteresting lines chomp; my @ary = (split /\s|\|/, $_) [3,21,24]; my $time = (split ' ', $ary[0])[4]; # first element ist timestamp, right? $SMPP_count{$time}++ if $ary[0] eq $stamp and $ary[1] eq "Submit" and $ary[2] =~ /(GSM|SMPP)/; }; } # now iterate over the keys of the hash to make up your sums for my $time ( sort keys %SMPP_count) { my $sum = $SMPP_count{$time}; ... }