# Use a stack to keep track of concurrent connections my @connections; while () { # Assume that seeing "Connection" before seeing "QUIT" # implies an interleaved connection record. if (/^Connection/) { ++$count_of_connections; push (@connections, $_); ++$count_of_overlaps if (scalar(@connections) > 1); } pop (@connections) if /^QUIT/ } print "$count_of_connections connections logged, $count_of_overlaps overlapped\n"; print "Leaving ", $count_of_connections - $count_of_overlapped, " clean connections logged.\n"