use warnings; my %data_sets; while(<>){ # I did the $line thing because there's some garbage at the top of the file that I don't want my $line++; if($line == 2){ while(/\G([^\t]+)/g){ $data_sets{$_} = {}; } } if($line > 3){ $data_sets{}; #<- ran into the problem right here! The script doesn't know what data set its on! }