Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Welcome to the Monastery
 
PerlMonks  

Re^2: Converting CSV to tab-delimited

by PhilHibbs (Hermit)
on Apr 14, 2008 at 14:22 UTC ( #680294=note: print w/ replies, xml ) Need Help??


in reply to Re: Converting CSV to tab-delimited
in thread Converting CSV to tab-delimited

Sure, I know that installing Perl modules isn't that hard (once you've tracked down a make program for Windows - and once you've learned to avoid anything that needs a C compiler) but some people just don't want to know. All you have to do is accidentally say "non-standard" and a seemingly-sane business analyst starts to have nightmares about viruses or hackers. I have learned to avoid anything that doesn't come bundled with ActiveState Perl (and thank the g0ds, they've started including Term::ReadKey!!!)

Most people here are Unix hackers, believe me working with Windows - and habitual Windows users - is really, really frustrating.

And there are no newlines in the files - I know, it's our software that's creating them.


Comment on Re^2: Converting CSV to tab-delimited
Re^3: Converting CSV to tab-delimited
by Erez (Curate) on Apr 14, 2008 at 17:45 UTC

    Text::CVS can be installed without a C compiler, and even without resorting to the command line, via ActiveState's ppm application.

    And there are no newlines in the files - I know, it's our software that's creating them.

    I suggest amending the introduction to the code and mentioning this, in case someone would want to use the code you posted and isn't sure whether there are newlines in the code.

    Software speaks in tongues of man.
    Stop saying 'script'. Stop saying 'line-noise'.
    We have nothing to lose but our metaphors.

      I suggest amending the introduction to the code and mentioning this,
      You're probably right. Done.
Re^3: Converting CSV to tab-delimited
by Anonymous Monk on Mar 16, 2009 at 22:40 UTC
    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://680294]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2014-04-20 21:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (487 votes), past polls