1

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 ?

mousey
  • 11,601
  • 16
  • 52
  • 59

2 Answers2

3

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
CanSpice
  • 34,814
  • 10
  • 72
  • 86
0
print ",,,,5th column\n";

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

Community
  • 1
  • 1
moinudin
  • 134,091
  • 45
  • 190
  • 216
  • your solution is not working for me. Nothing is being written. I think the problem is with the opening the file itself. Is this correct? – mousey Jan 11 '11 at 02:58
  • @mousey Have you included the file handle? The above was only demonstrating what to print. – moinudin Jan 11 '11 at 09:35
  • I included an +>>. its still not working. Now I am unable to read too. What might be the problem – mousey Jan 11 '11 at 17:49