in reply to Converting CSV to tab-delimited

Hello,

I just wanted to point out a bug in the original post by "PhilHibbs". That code mostly works, except for the handling of embedded double quotes. To fix this, change the line beginning with "if ($qv ..." to:

if (not $dq and $_ eq '"') { $dq=1; next; }

For convenience, here is a complete script that includes the bug fix and also writes the output to standard output, as opposed to writing to a file:

#!/usr/bin/perl ## Converts an Excel-style CSV-formatted file to TAB-separated format ## Note: doesn't handle newlines in quoted values use strict; use warnings; while (<>) { my $tab = ""; my $qv=0; # Quoted value indicator my $dq=0; # Double quote flag indicates the previous character was +a " for (split //) { # Start of a quoted value if (not $qv and $_ eq '"') { $qv=1; next; } # Double quotes within or at the end of a quoted value if (not $dq and $_ eq '"') { $dq=1; next; } # If last char was a double quotes OR we're not within a quoted +value, comma = tab if (($dq or not $qv) and $_ eq ',' ) { $dq=0; $qv=0; $_="\t"; } +# End of field # Two consecutive double-quote characters within a quoted value elsif ($dq and $_ eq '"') { $dq=0; } # Double double quotes $tab .= $_; } print $tab; }

Cheers,
---Reed