my @cache; my $found=0; while (<$file>) { if ( ... found the match ...) { $found++; } if (/^/) { @cache=(); } push(@cache, $_); if (/^/ and $found) { Do_stuff_with_match(@cache); $found=0; } }