if ($key eq $first ... $key eq $last) { push @keys, $key; if ($key eq $last) { # do something to process this record in @keys # assuming that there will be a new record # starting with $first on a subsequent line } }