if ($txtfile =~ m/(some associative pattern for each CSV list)/gs) { my $key = $1; while ($txtfile =~ m/(CSV)/gs) { push @{$hash{$key}}, $1 } }