daveinOK2004
MIS
Greetings and thanks in advance for any responses.
I'm new to Perl in particular and scripting in programming in general, and so far find it fascinating even if a bit frustrating at times.
I'm trying to write a PL file that will take one CSV file, parse it in (which I have done using TEXT::CSV. I then perform some manipulation with some of the fields and add a few of my own. I've gotten all of this working fine.
Now my final task is to write the new data to an output.csv file. I've got it working, with the exception that some of my fields may contain a comma, which breaks the output file.
#!/usr/bin/perl -w
use strict;
use warnings;
use Text::CSV;
my $infile = 'c:\work\input.csv';
my $outfile = 'c:\work\output.csv';
my $csv = Text::CSV->new();
open (CSV, "<", $infile) or die $!;
open (FH, ">", $outfile) or die "$!";
while (<CSV>) {
if ($csv->parse($_)) {
my @columns = $csv->fields();
**** bunch of manipulation code ****
print FH "$col1,$col2,$col3,$col4,$col5,$col6...$col36\n";
else {
my $err = $csv->error_input;
print "Failed to parse line: $err";
}
}
close FH;
close CSV;
Columns 3 and 5 are likely to contain one or more commas and break the whole output file. I know the fix is to put quotes around those columns, but don't know how to do that in Perl.
Can anyone help me out? Sorry for asking what is such a "dumb question".
Dave
I'm new to Perl in particular and scripting in programming in general, and so far find it fascinating even if a bit frustrating at times.
I'm trying to write a PL file that will take one CSV file, parse it in (which I have done using TEXT::CSV. I then perform some manipulation with some of the fields and add a few of my own. I've gotten all of this working fine.
Now my final task is to write the new data to an output.csv file. I've got it working, with the exception that some of my fields may contain a comma, which breaks the output file.
#!/usr/bin/perl -w
use strict;
use warnings;
use Text::CSV;
my $infile = 'c:\work\input.csv';
my $outfile = 'c:\work\output.csv';
my $csv = Text::CSV->new();
open (CSV, "<", $infile) or die $!;
open (FH, ">", $outfile) or die "$!";
while (<CSV>) {
if ($csv->parse($_)) {
my @columns = $csv->fields();
**** bunch of manipulation code ****
print FH "$col1,$col2,$col3,$col4,$col5,$col6...$col36\n";
else {
my $err = $csv->error_input;
print "Failed to parse line: $err";
}
}
close FH;
close CSV;
Columns 3 and 5 are likely to contain one or more commas and break the whole output file. I know the fix is to put quotes around those columns, but don't know how to do that in Perl.
Can anyone help me out? Sorry for asking what is such a "dumb question".
Dave