Hello Monks & Monkettes,

Here i am once again with another too hard for me problem ! I was given numerous csv files, and they use a comma as a field separator. To my surprise, some fields contain commas as a value. Since I intend to split on comas this kinda wrecks it all up. Those fields get enclosed in quotes, so one can see that the comas inside are values, and not a field separator.

Maybe I could use a csv module, but that's too easy ! But seriously, it got me thinking on this regexp problem :

# a 4 field coma separated csv line $l = 'w,ww,"a,bb,ccc,3 ,ee,",4' ;

How can i turn it into :

# a 4 field coma splitable csv line 'w,ww,"a-bb-ccc-3 -ee-",4' ;

I do not have a single idea so far ...
I guess it would be like running a regexp ( s/,/-/g ) on a match ( m/"(.*)"/ ), but i don't know how to do that.

Anyone knows how ? Thanks !


In reply to Replacing comas in a substring when it is between quotes by ZlR

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.