dmn001 has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I don't understand how to output with Text::CSV,

http://search.cpan.org/~makamaka/Text-CSV-1.20/lib/Text/CSV.pm#print

I am trying to output a simple array e.g. @array=qw( 1 2 3 4 5 'hello world');

to a CSV file.

Please can you help by posting a sample code doing the above - thanks.

Dave

OK - I think I understand it now -> please see the working example file:

http://dmn001.pastebin.com/0aSzkcFi

Regards,

Dave

Thanks ikegami and Tux for your help! :)

Replies are listed 'Best First'.
Re: text::csv output help
by ikegami (Patriarch) on Nov 30, 2010 at 20:18 UTC
    There's an example at the top of the link you posted. Just change @row to @array!
      thanks. btw, it has to be an array ref. $csv->print ($fh, $_) for \@rows; instead of @rows as it appears in the documentation.

        No, the documentation is correct.

        $csv->print($fh, $_) for @rows;
        and
        $csv->print($fh, \@row);
        make sense, but not
        $csv->print($fh, $_) for \@rows;

        I imagine that your @rows didn't actually contain rows.

Re: text::csv output help
by dmn001 (Initiate) on Dec 01, 2010 at 00:47 UTC
    What are the rows supposed to contain? How do you keep track of rows, they are all appearing on one line. Should I insert the "\n" character in somewhere? Otherwise I get the error: Expected fields to be an array ref at path line x. Say for example I wanted to make 2 rows of a CSV file, can you provide some code to do that? Regards, Dave