in reply to while (my @row = $sth->fetchrow_array)

First, the whole purpose of prepare/execute is so the sql api only has to parse the statement once -- well, that and the security concerns... So, I moved that up before the foreach().

map {} () is awesome.... try to get the hang of that. They suggest using as few print statements as possible, so I compounded that. I removed the undef @row, as I believe it's implied at the end of the block.

Lastly, if nothing is turning up in your output file, check your open() to see if you have the ">" in the filename.

Why did I rename all those default vars? Take it from someone that's been a perl nerd for a long time... it's better to name your perl vars longwindedly!

Meh, here's your snipped the way I might do it:

my $sth_C = $dbh->prepare( "select * from result_storage_keep where aggregated_area like ?") or die "Couldn't prepare query: " . $dbh->errstr; foreach my $area (@Geo_areas) { next unless $area; $sth_C->execute($area) or die "Couldn't execute query: " . $sth_C->errstr; while (my @row = $sth_C->fetchrow_array) { print OUTPUT_FILE join("\t", map {$_ ? $_ : "''"} @row), "\n"; } }

Also, since you seem to be printing it all to the same file anyway, why not shorten it even more and do something like this:

# this only works if this array is small-ish my $areas = join(", ", @Geo_areas); # interpolating can be dumb ... # unless you trust the data in @Geo_areas... my $sth_C = $dbh->prepare( "select * from result_storage_keep where aggregated_area in ($areas) order by aggregated_area") or die "Couldn't prepare query: " . $dbh->errstr; while (my @row = $sth_C->fetchrow_array) { print OUTPUT_FILE join("\t", map {$_ ? $_ : "''"} @row), "\n"; }