my @stuff; while () { my @fields = split $Delimiter, $_; my ($type, $name) = $fields[0] =~ /^(...)(.*)/; if ($type eq 'BNA') { # I don't know what other data you want to keep from the BNA line ... push @stuff, { name => $name, products => [], }; next; } # I don't know what you want to do with this, so I'll just keep it there. push @{$stuff[-1]{products}}, $_; }