in reply to Re: Replacing commas with pipes
in thread Replacing commas with pipes

Your second example turns a 3 column record into a 4 column record. Now, it matches the specification as given, but if you go the CSV way, it doesn't seem to be a logical solution.

Considering that Text::CSV_XS allows you to set the separator, it seems more logical to use two Text::CVS_XS objects: one to read, one to write.

Replies are listed 'Best First'.
Re^3: Replacing commas with pipes
by ww (Archbishop) on May 11, 2009 at 19:48 UTC
    re JavaFan's comment about toolic's "...second example":

    /me thinks that's a typo in data (d,e,"f|g" should be d,e,"f,g"), as OP's spec has commas throughout...even inside the quotes.

    Update: rephrased to clarify first line with attributions.

    Update2: In fairness, JavaFan's s/("[^"]*")|,/$1||"|"/eg; appears to work with this data:

    "x,y,z",red,blue 1,"2,3",4,foo,bar,"blivitz_kung" a,b,"c,d,e,",nodereaper "Super_Search","XP per node",data,"code tags,markup" monk,troll,saint,"holders of unholy powers","Orders of Monks:novice,in +itiate,etc"

    (Update3: bah, humbug, copied wrong data. Fixed above.)

    producing this output:

    "x,y,z"|red|blue 1|"2,3"|4|foo|bar|"blivitz_kung" a|b|"c,d,e,"|nodereaper "Super_Search"|"XP per node"|data|"code tags,markup" monk|troll|saint|"holders of unholy powers"|"Orders of Monks:novice,in +itiate,etc"

    Nonetheless, given the likelihood of (unknown, unspecified) edge-cases, a well-tested module remains the way to go, IMO.