mousey
mousey

Reputation: 11901

Need some help writing to a csv file

I am new to perl I need some help to write my output to the last column of the csv file after some operations on a single row. I am using Text::CSV_XS module in perl. I know the statement to write to a csv file is print FH "sitepoint, 2, 7\n" ; But how to write to a specific column like 5th column ?

Upvotes: 1

Views: 3909

Answers (2)

CanSpice
CanSpice

Reputation: 35790

From the Text::CSV docs:

When writing CSV files with always_quote set, the unquoted empty field is the result of an undefined value.

Thus, if you do something like:

my @arr = ( undef, undef, undef, undef, "5th column" );
my $csv = Text::CSV->new ( { always_quote => 1 } );
open my $fh, '>', 'outfile.csv' or die $!;
$csv->print( $fh, \@arr );

...you should get an output file of the form

,,,,5th column

Upvotes: 3

moinudin
moinudin

Reputation: 138337

print ",,,,5th column\n";

That will write to the 5th column. Each , shifts to the next column. Also read this.

Upvotes: 0

Related Questions