in reply to Double quotes in CSV using Text::CSV

Are you perhaps looking for something like this?

use Text::CSV; my $csv = Text::CSV->new({binary=>1,always_quote=>1,eol=>$/}); $csv->print(\*STDOUT, ["","",""]); __END__ "","",""

Replies are listed 'Best First'.
Re^2: Double quotes in CSV using Text::CSV
by Als1973 (Initiate) on May 19, 2015 at 11:10 UTC
    it's works, but always_quote=>1 it is no good. necessary for me:
    $csv->print(\*STDOUT, ["1","2",""]); __END__ 1,2,""

      I don't think this is possible with Text::CSV or Text::CSV_PP. The documentation of Text::CSV hints at maybe using ->is_quoted or the metadata information, but at least the implementation in Text::CSV_PP does not so any conditional quoting.

      That wasn't too difficult. See this commit. It will be available in version Text::CSV_XS version 1.18, but you can get it from github already.

      $ perl -Mblib -MText::CSV_XS \ ? -E'my $csv = Text::CSV_XS->new ({ quote_empty => 1 });' \ ? -E'$csv->combine (1, undef, "", " ", 2);' \ ? -E'say $csv->string' 1,,""," ",2

      update I forgot I already implemented this in the perl6 version Text::CSV.


      Enjoy, Have FUN! H.Merijn

      Your original post did not specify that. Please see I know what I mean. Why don't you? and How do I post a question effectively? Could you please show some sample input and output that is representative of what you are trying to print? Otherwise, we're just guessing at the correct answer.

      use Text::CSV; my $csv = Text::CSV->new({binary=>1,eol=>$/, quote_char=>undef,escape_char=>undef}); $csv->print(\*STDOUT, ["1","2",'""']); __END__ 1,2,""
        quote_char=>undef,escape_char=>undef helps Thanks

      WHY? Why can 1 and 2 not be quoted? I could add quote_empty => 1 to Text::CSV_XS, but you've shown me no good reason so far.

      "1","2","" is just as valid CSV as 1,2,"" and 1,2, are and on parsing they'll all yield the same data (unless parsing options cause different rules).


      Enjoy, Have FUN! H.Merijn

        Two ideas:

        Saving bytes
        Yes, despite having Gigabit networks and Terabyte harddisks, some people discuss every single byte.
        A primitive type system on top of CSV
        Unquoted numbers represent numbers, quoted numbers are just strings (as in JSON).

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)