Does the module also handle cases where the escape character may be
multiple?  For example,

        1,10\,000,"some text, and so","some text"

Eric

On Wed, 27 Mar 2002, Jenda Krynicky wrote:

> From: Bud Rogers <[EMAIL PROTECTED]>
> > Brian Volk wrote:
> > > Is there a way to covert a .csv file to .tab file? 
> > 
> > I would do something like this.
> > 
> > while (<>) {
> >         @fields = split(/,/,  $_);
> >         $line = join("\t", @fields);
> >         print $line;
> > }
> 
> While this might work for simple CSVs it's not safe. Consider this:
> 
>       1,2,"some text, maybe long","short text"
> 
> This is perfectly valid CSV, but your code will not only change the 
> separators, but also the value of the third column. The safest (and 
> also most general) way to convert between different formats of 
> CSVs is :
> 
> #!perl
> use IO::Handle;
> use Text::CSV_XS;
> 
> $reader = Text::CSV_XS->new({
>       quote_char => '"',
>       escape_char => '"',
>       sep_char => ','
> });
> 
> $writer = Text::CSV_XS->new({
>       quote_char => '"',
>       escape_char => '"',
>       sep_char => "\t"
> });
> 
> while ($columns = $reader->getline( *STDIN)) {
>       last unless @$columns;
>       $writer->print( *STDOUT, $columns);
>       print "\n";
> }
> __END__
> 
> Jenda

-- 

James Eric Lawson
Research Publications Editor
National Simulation Resource

[EMAIL PROTECTED]

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

Whereof one cannot speak, thereof one must be silent. -- Wittgenstein


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to