in reply to Re^2: Converting CSV to tab-delimited
in thread Converting CSV to tab-delimited
So, a hand-wired CSV solution is sought by those of us not in a position to "simply ppm or CPAN Text::CSV into place". Good material is sparse - even the CookBook example isn't all that great. I did track down a regex which I have needed to follow up with several checks and edits to patch things up...
This then is a starting point (ugly/rough code):
my @inList = split /,(?!(?:[^",]|[^"],[^"])+")/; # and further on a bit of a mess: my @outList = (); for (my $i=0; $i<$flds; $i++) { if (! defined $inList[$i] ) { $inList[$i] = ""; } if ($inList[$i] =~ m/\D/) { $inList[$i] = '"'.$inList[$i].'"'; } $inList[$i] =~ s/^""/"/; $inList[$i] =~ s/""$/"/; $inList[$i] =~ s/^"$/""/; push @outList, $inList[$i]; }
I eventually got to a point with my data that I simply sanitize all the crap in a field like ",", "'" and """ in self defense, straight after dealing with any nulls.
I hope this is useful for someone.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Converting CSV to tab-delimited
by Anonymous Monk on Mar 18, 2009 at 15:22 UTC |