my @captured; While I still have records: Is this a break line? Y - check_for_more_breaks(@captured) @captured = () next item N - save line to @captured Anything left in @captured? check_for_more_breaks(@captured) sub check_for_more_breaks: # Your actual processing here is unclear; I've taken # a guess at it. my @subgroup; my $last_sequence_number; for each line: extract the fields is last_sequence number defined? N - save this one as the last sequence number save the current data of interest in @subgroup next else is there a gap in the sequence numbers? Y - summarize(@subgroup) @subgroup = (); save current sequence number as last save the current data of interest next