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"; }

In reply to Re: while (my @row = $sth->fetchrow_array) by jettero
in thread while (my @row = $sth->fetchrow_array) by Win

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.