Sure -- you have the colnames in @cols and the values in @avgs, so can just loop over them.
my @avgs = $dbh->selectrow_array($avgSQL);
# CSV output method A
print join(",", @cols) . "\n";
print join(",", @avgs) . "\n";
# CSV output method B (2-columns)
print "Col,Avg\n";
printf("%s,%f\n", $cols[$_], $avgs[$_]) for 0 .. $#cols;
Note you can adjust the printf format according if you want a fixed number of decimal places or whatever.
And one more way to do the same thing, which i introduce because it could be handy if you're going to process these avgs at all later in your code. And that is to hash them up for easy access:
my %avgsHash;
@avgsHash{ @cols } = @avgs; # uses a hash slice to populate %avgsHash
print "Col,Avg\n";
printf("%s,%f\n", $_, $avgsHash{$_}) for @cols;
|