in reply to Converting File Delimiters

$s = 'aaa,bbb,"ccc, ddd", fff'; print join '|', $s =~ m[("[^"]+"|[^,]+)]g; aaa|bbb|"ccc, ddd"| fff

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

The start of some sanity?

Replies are listed 'Best First'.
Re^2: Converting File Delimiters
by frozenwithjoy (Priest) on Aug 09, 2012 at 00:09 UTC
    That is so much nicer than what I came up with, but I'll add mine, too (otherwise it would have felt like a total waste of time):
    #!/usr/bin/env perl use strict; use warnings; use feature 'say'; my $string = qq(aaa,bbb,"ccc,ddd",fff,ggg,"hhh,iii",jjj); my @split_on_comma = split /,/, $string; my @quoted; my @ready_to_join; for (@split_on_comma) { if ( /^"/ .. /"$/ ) { s/"//g; #remove this if you actually want "s in your output push @quoted, $_; } else { push @ready_to_join, join ',', @quoted if scalar @quoted; @quoted = (); push @ready_to_join, $_; } } my $pipe_delim = join '|', @ready_to_join; say $pipe_delim;
    Output: aaa|bbb|ccc,ddd|fff|ggg|hhh,iii|jjj

      This code is what I want, but how do I get it to use a file for input and output?

      I have tried various revisions but not having any luck

      Thanks, Mike

Re^2: Converting File Delimiters
by BrowserUk (Patriarch) on Aug 10, 2012 at 00:26 UTC

    The above as a one-liner

    perl -ple"$_ = join '|', m[(\x22[^\x22]+\x22|[^,]+)]g" in.csv > piped. +csv

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

Re^2: Converting File Delimiters
by hbm (Hermit) on Sep 21, 2012 at 23:43 UTC

    On a client's system, where I'd have to run the change management gauntlet to install Text::CSV, I remembered this node. I had to adjust it to handle empty numeric fields (,,,):

    $_ = '"",,,"abc",0,,'; print join '|', m/ "[^"]+" # quoted string | [^,]+ # or non-commas | (?<=,)(?=,|$) # or nothing, surrounded by commas or EOL /gx; # prints: ""|||"abc"|0||

      Nice extension!


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      RIP Neil Armstrong