perl generate csv file

hi..
i have this perl code that create csv files as well as text files.
however, my csv fils could only print 1 output instead of 2.

how can i modify the below code to include the
foreach my $r(@$d) {
print FILE join("\t", @$r), $/;}

to the csv output.


this is my code
my($csv) = Text::CSV->new;
open( CSV, ">$file.csv") || die "Failed for $file.csv: $!\n";

# Write Header
$csv->combine( qw/Qin Qout Yield Reject/);
print CSV $csv->string . "\n";

# Write data using foreach or some other loop.

  $csv->combine( @{$t} );
  print CSV $csv->string . "\n";

close( CSV );


die "$file already exists\n" if -f $file;
open FILE, ">$file" or die "Can not create $file $!\n";

#print the sums at the top of the file, separated by tabs
print FILE "Daily Yield Report Summary\n";
print FILE "Device: '$dev'\n";
print FILE "Date: '$datef' to '$datet'\n\n";
print FILE "Date\t\t", "Qin\t", "Qout\t", "Yield\t", "Reject\t", "Lot\n";
foreach my $r(@$d) {
print FILE join("\t", @$r), $/;}

#write the other arrays in tab-separated columns underneath the sums
 print FILE  "-"x60, $/;
 print FILE "Total\t\t", join("\t", @$t), $/;

# Graph
print FILE $graph->to_string( [("\t", @$t), $/],
                           labels => [ qw/, ,Qin, Qout, Yield, Reject/ ],
                         );

close(FILE);
}
}
TPolyAsked:
Who is Participating?
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.

ozoCommented:
die "$file already exists\n" if -f $file;
open FILE, ">$file" or die "Can not create $file $!\n";
#this still bothers me.
#I would recommend using sysopen if there's any chance that
#two instances of this program may be running at the samee time.



foreach my $r(@$d) {
    print FILE join("\t", @$r), $/ or warn $!;
    $csv->combine( @{$r} );
    print CSV $csv->string . $/ or warn $!;
}
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
TPolyAuthor Commented:
it works
but juz wanna ask abt this "error" msg:
Bad file descriptor at graphtxtcvs.tk line 379.

this is my line 379
    print FILE join("\t", @$r), $/ or warn $!;

thanks
0
ozoCommented:
Are you closing FILE in the loop?
0
TPolyAuthor Commented:
this is my code..
i do not find any loop in it?


#Create Graph
my $graph = Text::Graph->new( 'Bar' );
  print $graph->to_string( [("\t", @$t), $/],

                           labels => [ qw/,,Qin, Qout, Yield, Reject, Lot/ ],
                         );

my($csv) = Text::CSV->new;
open( CSV, ">$file.csv") || die "Failed for $file.csv: $!\n";

# Write Header
$csv->combine( qw/Date Qin Qout Yield Reject Lot/);
print CSV $csv->string . "\n";

# Write data using foreach or some other loop.
foreach my $r(@$d) {
    print FILE join("\t", @$r), $/ or warn $!;
    $csv->combine( @{$r} );
    print CSV $csv->string . $/ or warn $!;
}  
$csv->combine( Total, @{$t} );
  print CSV $csv->string . "\n";


close( CSV );


die "$file already exists\n" if -f $file;
open FILE, ">$file" or die "Can not create $file $!\n";

#print the sums at the top of the file, separated by tabs
print FILE "Daily Yield Report Summary\n";
print FILE "Device: '$dev'\n";
print FILE "Date: '$datef' to '$datet'\n\n";
print FILE "Date\t\t", "Qin\t", "Qout\t", "Yield\t", "Reject\t", "Lot\n";
foreach my $r(@$d) {
print FILE join("\t", @$r), $/;}

#write the other arrays in tab-separated columns underneath the sums
 print FILE  "-"x60, $/;
 print FILE "Total\t\t", join("\t", @$t), $/;

# Graph
print FILE $graph->to_string( [("\t", @$t), $/],
                           labels => [ qw/, ,Qin, Qout, Yield, Reject/ ],
                         );

close(FILE);
}
}

0
ozoCommented:
You don't seem to open FILE before
print FILE join("\t", @$r), $/ or warn $!;

use Fcntl;
sysopen FILE,$file,O_CREAT|O_EXCL|O_WRONLY or die "$file $!";

will only open the file if it does not exist.
if two programs try it at the same time, only one will win.
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.