Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
Do you know where your variables are?
 
PerlMonks  

CSV_XS issue

by anaconda_wly (Scribe)
on Apr 07, 2013 at 09:56 UTC ( #1027345=perlquestion: print w/ replies, xml ) Need Help??
anaconda_wly has asked for the wisdom of the Perl Monks concerning the following question:

code as below: my row1 and row2 were printed on the same line when I opened the csv file, not in TWO ROW as I expected, why? Thanks for help

use Text::CSV_XS; my ($csv, $fh); &main; sub main { my @row1 = ("aaa","fff","sss" ); my @row2 = ("bbb","fff","sss"); $csv = Text::CSV_XS->new ( { binary => 1 } ) # should set binary att +ribute. or die "Cannot use CSV: ".Text::CSV->error_diag (); open $fh, ">:encoding(utf8)", "new.csv" or die "new.csv: $!"; $csv->print ($fh, \@row1); $csv->print ($fh, \@row2); close $fh or die "new.csv: $!"; print " finished \n"; }

Comment on CSV_XS issue
Download Code
Re: CSV_XS issue
by Corion (Pope) on Apr 07, 2013 at 10:01 UTC

    It's a bit weird that this is not the default, but comparing your code with the synopsis section of Text::CSV_XS shows this one line:

    $csv->eol ("\r\n");

    As soon as you add that, you'll get separated lines.

Re: CSV_XS issue
by Tux (Monsignor) on Apr 07, 2013 at 10:04 UTC

    What Corion said:

    $csv = Text::CSV_XS->new ({ binary => 1, eol => "\n", auto_diag => 1 } +);

    The eol attribute is only required on output. The fact that it doesn't default to $/ is because of backward compatibility. It was the way it is when I took over.


    Enjoy, Have FUN! H.Merijn
      yes, it works, thanks you both! Good to know for the history!

        Just to add a bit to clarify and keep history, as Corion asked me in /msg: the $csv object is used for both input/getline/parse and output/print/combine. Most of the questions I get are for parsing, and most of my own scripts are parsing CSV data.

        The parsing code works better when not setting eol, as it then allows more "default" values (to be mixed) and works faster (but probably not measurable).

        Setting eol to $/ by default would most likely break a lot of parser scripts. Setting eol to $/ on output only makes some sense, but also makes the documentation harder and I wonder if it would be worth breaking backward compatibility.

        What does make sense in breaking backward compatibility is to make binary default to a true value, as I can't think of a modern situation where it would break a parser.


        Enjoy, Have FUN! H.Merijn
        cc
Re: CSV_XS issue
by talexb (Canon) on Apr 07, 2013 at 13:52 UTC

    Finally, from a stylistic point of view, you're calling main and then declaring it a line later .. you can just skip both of those steps. Here's how I would re-format your code..

    use strict; use warnings; use Text::CSV_XS; { my @row1 = ("aaa","fff","sss" ); my @row2 = ("bbb","fff","sss"); # should set binary att my $csv = Text::CSV_XS->new ( { binary => 1 } ) or die "Cannot use CSV: ".Text::CSV->error_diag (); open my $fh, ">:encoding(utf8)", "new.csv" or die "Failed to open new.csv for writing: $!"; $csv->print ($fh, \@row1); $csv->print ($fh, \@row2); close $fh or die "Failed to close new.csv: $!"; print " finished \n"; }
    • I've added the use of strict and warnings -- the Perl compiler should be happy with your code;
    • I'm declaring the variables at the point at which they're used, rather than at the top of the program;
    • I've expanded the error messages to be more informative; your version would show the same error for both open and close errors;
    • I've spaced things out to make the code more readable -- I'm a fan of blank lines to separate chunks of code;

    You could get more cleanup by using perltidy -- I'm a fan of this utility.

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

      Yes, thanks for the reformatting. I used to be c\c++ programmer. So I have some habbit incompatible with the Perl like the main and variable declaration and I'm in haste by the little snippet to find the correct usage. Good to know the perl style programming and your experience sharing. That's really cool to me!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1027345]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2014-04-20 22:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (489 votes), past polls