In the code below, I am replacing the comma with a dash in the quoted string in each input line. The code works, but I'm wondering if there is a way to do this in one statement. I tried a few variations but the stumbling block was always that the "capture" variable $1 is read-only.

BTW, before someone mentions using Text::CSV_XS, I have a version that uses the module but it takes over 4 times longer to process. The real files I will be processing will be huge and I need all the speed I can squeeze out of this.

FYI: The reason I am doing this is because SQL Server BULK INSERT does not respect quoted values. It sees the comma in the quoted string and treats it as a field terminator. :(

#!/usr/bin/perl use strict; use warnings; while (<DATA>) { chomp; m/"([^"]+)"/; # Extract quoted value. my $val = $1; # Save value in variable. $val =~ s/,/-/; # Change comma to dash. s/"[^"]+"/$val/; # Replace quoted value. print $_, "\n"; } __DATA__ 65722417,"1193,1",7980,1133566,4169735,035,FEDERAL UNIVERSAL SERVICE F +UND,0.12998 65722417,"1193,1",1012,1132900,4150053,C2,Carrier Cost Recovery Fee,0. +0273

"Its not how hard you work, its how much you get done."


In reply to Modifying CSV File by roho

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.