in reply to Re: how to modify a field in a CSV file
in thread how to modify a field in a CSV file

Peter, Thanks for the suggestion. My data is quite flexible and I only provided an example for illustrative purposes. My goal is to modify a field in a CSV file. The input to my script is the field (index) to modify and the change to the field. As previously mentioned, it is easy to parse a CSV file using text:csv_xs and get the field that the user wants to change and then make the change. My only problem is that I've not figured out how to write the new file with just the change to the field. In other words, I'm trying to retain the double quotes and the comma delimiters that were part of the input file. I'd like to think that I can do this with text::csv_xs but I've not figured out how to do it yet so that is the reason for this post
  • Comment on Re^2: how to modify a field in a CSV file

Replies are listed 'Best First'.
Re^3: how to modify a field in a CSV file
by Eliya (Vicar) on Sep 02, 2011 at 18:32 UTC
    I'm trying to retain the double quotes and the comma delimiters that were part of the input file. I'd like to think that I can do this with text::csv_xs

    Sure, see combine().

    my $csv = Text::CSV->new ( { quote_space => 0 } ) or die "Cannot use CSV: ".Text::CSV->error_diag (); my @columns = ("What, a nice day", "This is a fence", "Here is an,and +day"); print $csv->string() if $csv->combine(@columns);

    generates the following line:

    "What, a nice day",This is a fence,"Here is an,and day"
      That did it! Thank you so much for cluing me into to the combine function. You are great and the next beer I drink is in your honor!

        Or use print when setting eol attribute

        my $csv = Text::CSV->new ({ quote_space => 0, eol => "\n", auto_diag = +> 1 }); my @columns = ("What, a nice day", "This is a fence", "Here is an,and +day"); $csv->print ($fh, \@columns);

        Enjoy, Have FUN! H.Merijn