my $window = 300; # 5 min. my @errors; my @timestamps; while () { my $timestamp = get_timestamp($_); while (!@timestamps or $timestamps[-1] < $timestamp + $window) { if (defined(my $err = )) { push @errors, $err; push @timestamps, get_timestamp($err); } else { last; } } while (@timestamps and $timestamps[0] < $timestamp - $window) { shift @errors; shift @timestamps; } for my $error (@errors) { # check if access line $_ matchs # with error line $error here: ... } }