use strict; my %ismatached = (); while () { if (/Start Query \[ID:\s*(\d+)\s*\]/) { $ismatched{$1}++; } elsif (/End Query \[ID:\s*(\d+)\s*\]\s*\[duration:\s*(\d+)\s*\]/) { my $id = $1; $ismatched{$id}+= 2; my $duration = $2; if ($duration > 300) { print "Look at Id: $id -- took longer than it should.\n"; } } } foreach (sort keys %ismatched) { if ($ismatched{$_} == 1) { print "Process id $_ never ended.\n"; } elsif ($ismatched{$_} == 2) { print "No record of process id $_ ever starting.\n"; } else { delete $ismatched{$_}; } } print "There were a total of ", scalar(keys %ismatched), " unmatched processes.\n";