Export database table to a csv file in perl

If my table is like the following:-

Name Age
John 10
Luke 20
Roger 30

What I would like to do is to output above to a csv file in the following format:-

John, Luke, Roger
10, 20, 30

I have the table in an array which i assume needs to be manipulated…
Any ideas how i can do this ?

as you read the data from the databse, store the names in one array and the numbers in another array. Then use those arrays to output to the cvs file. Post your current code.

my $sql = ‘select job_name, run_rime from batch_timings
where job_name in (“anv.pr.ia.rbs.elp”,“anv.pr.birt.batch.elp”)’;

 $getkey = $dbh -> prepare($sql);
 $getkey -> execute;

 open OUT, ">$rfile" or die "Can't open $rfile. $!\


 while (@row = $getkey->fetchrow) {
            foreach (@row) {
            print OUT $_ . "\

" ;

 close OUT;

Umm… this might work:

my $sql = 'select job_name, run_rime from batch_timings where job_name in ("anv.pr.ia.rbs.elp","anv.pr.birt.batch.elp")';

my $getkey = $dbh->prepare($sql);

my (@names, @values);

while( my @row = $getkey->fetchrow_array ){
    push @names, $row[0];
    push @values, $row[1];


open my $OUT, '>', $file or die $!;
print {$OUT} (join ",", @names) . "\
print {$OUT} (join ",", @values) . "\
close $OUT;

NOTE: The code is untested, but should work.

Thanks for that.

I have another question :-

What can we use in perl to retreive a date in the Locale format?

Basically, I am retrieving some data from a database table via a stored proc. The data in the table is in English date format i.e dd/mm/yyyy. However, the perl scrip somehow converts this into mm/dd/yyyy.

I have used setlocale(LC_ALL, “english”); - but not doing the job.

Any ideas what I can do here ?