Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
Perl Monk, Perl Meditation
 
PerlMonks  

Need to create CSV using Text::CSV

by oldwarrior32 (Sexton)
on Jun 17, 2012 at 01:48 UTC ( #976614=perlquestion: print w/ replies, xml ) Need Help??
oldwarrior32 has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, I want to create a CSV File using Text::CSV. I was able to do the thing by hand but I would like to create it using the module.

I have this:

my @AoA= (['a', 'b'], ['c', 'd'], ... and so on); my $csv = Text::CSV->new ( { binary => 1 } ); open my $fh, ">", "new.text"; $csv->print ($fh, $_) for @AoA; close $fh;

I got the following input:

a,b,c,d,e,f....etc.

I was hoping something like this:

a,b

c,d

e,f

etc...

When i use the Text::Table module, I pass to the load function the @AoA array, and I got a nice table. But with the Text::CSV module doesn't work.

I read the Text::CSV doc and It says I need to pass an array reference to the print function. But if I do that I got this:

my @AoA= (['a', 'b'], ['c', 'd'], ... and so on); my $aref = \@AoA; my $csv = Text::CSV->new ( { binary => 1 } ); open my $fh, ">", "new.text"; $csv->print ($fh, $aref) for @AoA; $csv->print ($fh, $aref);#tried this too. close $fh

Ouput:(0x146ad74),ARRAY(0x1496524),ARRAY(0x14448ec),ARRAY(0x1157ec4),etc..

What I am doing wrong?

Thanks for your help!

Comment on Need to create CSV using Text::CSV
Select or Download Code
Re: Need to create CSV using Text::CSV
by frozenwithjoy (Curate) on Jun 17, 2012 at 03:07 UTC
    Instead of passing elements of your array to print, just use a refernce to the entire array. Does that fix it?

      I tried that but the output is a CSV of array references(see the ending of my post).

Re: Need to create CSV using Text::CSV
by Athanasius (Prior) on Jun 17, 2012 at 03:22 UTC

    Hi oldwarrior32,

    Change the call to new as follows:

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

    (You will have to adjust the "\n\n" to meet your exact requirements.)

    HTH,

    Athanasius <°(((><contra mundum

      I tried but it didn't worked.

      But, and because your illumination, I used:

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

      and it worked!

      Thanks very much!

      PD: next time I'll try to read the whole doc first.

      That is surely not going to work correctly. It will create an extra empty line in between each record.

      Update: forget the above. I misread the requirements. Thanks Athanasius for pointing it out.

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      My blog: Imperial Deltronics
        It will create an extra empty line in between each record.

        Yes, exactly. The OP wrote:

        I got the following input:

        a,b,c,d,e,f....etc.

        I was hoping something like this:

        a,b

        c,d

        e,f

        etc...

        showing a blank line between each pair of output lines — from which I took it that the extra line was a requirement. But see the caveat at the end of my post.

        Regards,

        Athanasius <°(((><contra mundum

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (12)
As of 2014-04-16 18:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (433 votes), past polls