Export PERL Array data to CSV

Hello,

I have a simple PERL script that connects to an Oracle DB, runs a SQL query and prints the array results into a table.  I need to give the option to export this array to a csv file.  What is the easiest way to add this functionality?  This is my  array output from the script after the SQL statement:

$sth->execute;
my $names = $sth->{'NAME'};
my $tbl_data = $sth->fetchall_arrayref;
print $cgi->table({-border=>1,-align=>'CENTER',},
           $cgi->Tr({-align=>'CENTER',-valign=>'TOP',-BGCOLOR=>'cccccc'},
           [
              $cgi->th($names),
              map { $cgi->td($_) } @$tbl_data,
           ]
           )
        );

Thanks for you help!
Tbone3434Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

wilcoxonCommented:
There is no really easy way to do it.  You'll need to create a second output routine that does the same data fetch from the database (or move the html output into a sub) and outputs as csv (rather than html).

For the output to csv code, it's just:

print join "\n", map { join(',', map { "\"$_\""' } @$_ } @$tbl_data;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Tbone3434Author Commented:
Thanks for you help on this Wilcoxon!  I am havinf some difficulty getting that to work though.  What else am I missing on this?  Even if this could just print to a CSV file on the server each time it ran rewriting the old data and a user could just click a link and download the file?  what do you think?  Again thanks for your help! :)
#!/usr/bin/perl

use DBI;

my $dbh = DBI->connect('dbi:Oracle:test','user','password',
                      { RaiseError => 1,AutoCommit => 0})
              or die "Database connection not made: $DBI::errstr";
{
my $sth = $dbh->prepare("SELECT * FROM test_name");
$sth->execute;
my $names = $sth->{'NAME'};
my $tbl_data = $sth->fetchall_arrayref;
print join "\n", map { join(',', map { "\"$_\""' } @$_ } @$tbl_data;


$dbh->disconnect(); }

Open in new window

0
wilcoxonCommented:
When you say you are having difficulty, what is it you are having trouble with?  Does the script produce the CSV file correctly and just isn't working as a CGI?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.