http://www.perlmonks.org?node_id=1230086


in reply to Multiple queries on DBI corresponding to multiple csv files?

I'll leave it to others to comment about using other modules to write csv files so proper quoting is performed and just discuss your imediate question

This is untested, demonstration code

use strict; use warnings; use DBI; my @clumps=( {table=>'tb1',file=>"csvfile1.csv"}, {table=>'tb2',file=>"csvfile2.csv"}, {table=>'tb3',file=>"csvfile3.csv"} ); my $dbname='a'; my $user='b'; # DBI CONNECTION my($dbh) = DBI->connect("dbi:Ingres:$dbname","$user","") or die "Could not connect to database $dbname\n"; for my $clump (@clumps){ my ($sth) = $dbh->prepare('SELECT * FROM '.$clump->{table}) or die "Prepare failed: $DBI::errstr\n"; $sth->execute() or die "Prepare failed: $DBI::errstr\n"; open my $fh, ">raw", $clump->{file} or die $clump->{file}. ": $!"; $fh->print (join(",", @{$sth->{NAME}}), "\n"); #show header while (my @row = $sth->fetchrow_array()) { $fh->print (join(",", @row), "\n"); }; close $fh or die $clump->{file}.": $!"; $sth->finish(); } $dbh->disconnect();
Notice how i used an array of hashes to contain the table name and outfile file, then cycled thru it to do each table/outfile file pair

Edit: Hippo was right "You've set @clumps as an arrayref but then treat as an array. Typo?"

he saw it as

my @clumps=[ {table=>'tb1',file=>"csvfile1.csv"}, {table=>'tb2',file=>"csvfile2.csv"}, {table=>'tb3',file=>"csvfile3.csv"} ];