All well and fine. I have a situation again where ROOTman (as we will call him) does not like anyone adding Perl modules to the production server. Though he's a Perlist himself, requests for additional modules are rebuffed as he cannot risk this machine having a hiccup. We also may be tied to an older release for quite some time, until the new hardware arrives and then we get whatever Perl comes on the RH Enterprise install.
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. |