foreach (@regions_A_array){ my $Select_two_records = " SELECT G.Region, G.Score FROM Gov_regions_scores_TEMP G, Region_lookup R WHERE R.String = '".$_."' AND RTRIM(R.Place) = RTRIM(G.Region) "; my $sth_m_A = $dbh->prepare($Select_two_records) or die "Couldn't prepare query: ".$dbh->errstr; $sth_m_A->execute() or die "Couldn't execute query: ".$sth_m_A->errstr; my @regions_A; while (@regions_A = $sth_m_A->fetchrow_array) { print OUTPUT $_; print OUTPUT "\n"; } }