First my rant: No offense monks, but some of the answers I've read to this question are kind of pathetic. I don't care whether or not there is a module or not, but sometimes it is helpful to know the technique. Sheesh. Feel free to make comments about using modules, but at least give him/her some direction besides, "No one does this anymore, just use the module or forget it!" Reinventing the wheel is a time-honored process for learning. Nice way to be rude to a potential JAPH.

Anonymous, my first suggestion is to browse CPAN for the modules that have been mentioned already by others. At http://search.cpan.org/ you can view the documentation of modules and then click on the "Source" links to view the actual source. Then, you can see how the module author has done it.

If you are interested in really doing this yourself, which I, like the others, would not recommend. You can use a negative lookup in a split to use escaped commas:

my @fields = split /(?<!\\),/, $line;

Another solution is to use a character which you do not allow in your strings, which is why characters like ^ or ~ or | are often used as delimiters in CSV files.

The most common solution, however, is to use quotes around strings to escape an entire string. That is, commas are only available for splitting outside of quotes. This is difficult enough that I don't, off-hand, have a simple solution, though I've seen a few...for this, I would definitely suggest browsing the source of a pertinent module.


In reply to Re: Reading escaped data from a CSV by hanenkamp
in thread Reading escaped data from a CSV by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.