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

=========== [EMAIL PROTECTED] == http://Jenda.Krynicky.cz ==========
There is a reason for living. There must be. I've seen it somewhere.
It's just that in the mess on my table ... and in my brain
I can't find it.
                                        --- me

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

Reply via email to