my $entry = ""; while () { if(/^($monthRegex)\s+d{1,2}\s+(\d{2}:){3}/) { $result = &handleEntry( $entry ) if $entry; $entry = $_; # unless you want to ignore some of'em # in which case: $entry = ""; } else { $entry .= $_ if $entry; } } $result = &handleEntry( $entry ) if $entry; # and replace the "foreach" loop in the previous version # with "sub handleEntry { ... }"