open(B, '<', 'fileB'); while () { chomp; my @cells = split(' ', $_); # $cells[0] is the state name, $cells[1] is the capital my $data_from_a = $data{lc($cells[0]} || []; print join("\t", @cells, @$data_from_a), "\n"; }