$SQL = "select userid a, giftid b ....";
. . .
print F "$row_ref->{$_} " for sort keys %$row_ref;
####
print F "$row_rf->{$_} for qw( userid gifid );
####
while ($row_ref = $ST->fetchrow_arrayref) {
print F "$row_ref->[$i] " for 0 .. $#{$row_ref};
####
print F join(" ", @$row_ref);