?
Solved

perl generate csv file

Posted on 2004-11-03
5
Medium Priority
?
9,834 Views
Last Modified: 2008-01-09
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);
}
}
0
Comment
Question by:TPoly
  • 3
  • 2
5 Comments
 
LVL 85

Accepted Solution

by:
ozo earned 500 total points
ID: 12490697
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
 

Author Comment

by:TPoly
ID: 12562270
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
 
LVL 85

Expert Comment

by:ozo
ID: 12562379
Are you closing FILE in the loop?
0
 

Author Comment

by:TPoly
ID: 12562420
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
 
LVL 85

Expert Comment

by:ozo
ID: 12563135
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

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question