First off, use a CPAN module to parse the delimited files, be it Text::CSV, Text::CSV_XS, or tilly's utra versatile Text::XSV.

Quoted every word that is not already quoted is rather tough problem. Here is a trivial solution:

$word = qq("$word") if $word !~ /"/;
but this only handle words, no digits. Also, what if there is one quote present? or something like this:
foo,hello""world,bar
Gets rather sticky, which brings me to your next question, How to determine the delimiter being used? Well, there is no solution for that - partial solutions, maybe - but i would use my own eyes for that solution.

Ever hear the one about about the $600 pen delevoped for space. It was painstakenly and expensively designed to work in zero gravity and extreme temperatures. The Russians used a pencil! :D

Lesson here, some things are quicker done by brute force.

Last question, how to pull out each word? Read the docs for one the CPAN modules mentioned above. There have been LOTS of questions asked on this site about parsing CSV files. Try super search on this site. i won't show you code, because i would just be copying the docs, but i will explain the process.

Basically, you open the file and read it one line at a time or slurp it into an array and process the lines that way (hint: while loop). Then you pass each line to a method provided by one of the CPAN modules mentioned above, and you get back a list of parsed scalars. You process those one at a time (hint: for loop) and determine if you need to add quotes or not (hint: regex).

What to do from there you never specfied, i would imagine the most usefull thing to do would be to save it back to another file.

jeffa


In reply to (jeffa) Re: Quoting words by jeffa
in thread Quoting words by ellem

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.