in reply to csv to flat-file

Why not just use a substitution to convert commas to spaces:

$line =~ s/,/ /g;

Alternatively split the fields up and join them back together:

my @tmp_array = split(',',$line); $line = join(' ',@tmp_array);
or more compactly but less readably
$line = join(' ', split(',',$line));

Replies are listed 'Best First'.
Re: Re: csv to flat-file
by jreades (Friar) on Nov 28, 2002 at 09:20 UTC

    All of these solutions are great, but they all assume that you can't have a comma as part of the field value.

    Given that this is almost always possible, you have two choices:

    1. If commas as part of the field are escaped, then you can use a negative look-behind assertion:split(/(?<!\\),/, $line)
    2. If commas are not escaped, but can form part of a field delimitted with quotation-marks of some kind (" or ') then things get a lot uglier. I believe that there's a regex that could do it, but I could never figure it out and had to switch to a while loop and essentially chew through a line.

    HTH

Re: Re: csv to flat-file
by Cockneyphil (Acolyte) on Nov 28, 2002 at 09:21 UTC
    The question hasn't displayed properly (it did on the preview though) the output i'm after is xxx yyy (on a new line) zzz (on a new line)

      OK. In the substitution replace the comma witha newline character: s/,/\n/g;

      The split solution could become:

      $, = "\n"; print split(',',$line);

      ($, = "\n"; sets the character used to separate entries when print is used to print an array, as returned by split)

      As pointed out, all these solutions fail if any entries can contain a comma, in which case you should investigate the modules suggested elsewhere. I was assuming that since the format of your example was simple you would not have such complex data.