http://www.perlmonks.org?node_id=486690


in reply to Re^2: Textfile to csv with a small twist
in thread Textfile to csv with a small twist

But it doesn't do any CSV parsing - it just reads lines. What exactly would you do with a "CSV parsing module that's capable of recognizing and handling embedded newlines, embedded quotes, and other features hand-rolled CSV parsing usually miss"? There are only text lines to read, and only CSV lines to produce. I was just illustrating a method of reading data and transposing it for output.



pbeckingham - typist, perishable vertebrate.

Replies are listed 'Best First'.
Re^4: Textfile to csv with a small twist
by jZed (Prior) on Aug 25, 2005 at 19:12 UTC
    Your code has this:
    print $data{$columns[$c]}[$i]; print "," if $c < $#columns;
    That attempts to construct a CSV record by merely putting commas between fields. If there is anything in the field (for example the newlines the OP requested), then you will not end up with a valid CSV file. Text::CSV_XS has the combine() method that will properly create records by not only inserting commas, but also, when called for quoting and escaping the field data. Other CSV modules like Text::xSV have similar methods to not only parse CSV correctly, but to produce it correctly.