[TriLUG] Perl CSV

Brian McCullough bdmc at buadh-brath.com
Thu Nov 14 14:21:37 EST 2013


I am sure that somebody here has some experience with the Perl Text::CSV
mondule.

I have used it in the past, even successfully, but I remember it being
very picky about the input.

I am trying to use it again, but every row, after $csv->parse, is
reporting that there is only one element in the array.

Just to cut down on the rotation, here is a snapshot of my code:


my $csv = Text::CSV->new ( { binary => 1 } )  # should set binary attribute.
            or die "Cannot use CSV: " . Text::CSV->error_diag () ;

while ( my $row = $csv->getline( $fh ) ) {
        printf "row = [" ;
        $csv->print( $logh, $row ) ;
        printf "]\n" ;

	$status = $csv->parse( $row ) ;
	@cols = $csv->fields ;
	$col_cnt = @cols ;

        $data_year = $cols[ 0 ] ;

printf "status = $status\n" ;
printf "col_cnt = $col_cnt\n" ;
printf "\ndata_year = [$data_year]\n\n" ;

 }


"status" comes out as "1", and so does "col_cnt".  Printing "row", however, shows the 35 or so columns in the CSV file.

"data_year" reports that it is an Array.


Suggestions for debugging?


Thanks,
Brian



More information about the TriLUG mailing list