Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

How do I create a CSV from a 2D array?

by bari (Novice)
on Dec 14, 2000 at 04:30 UTC ( [id://46529]=perlquestion: print w/replies, xml ) Need Help??

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

I have an array-of-arrays, containing numbers and strings, and I want to represent it as Comma-Separated Values and Tab-Separated Values.

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I create a CSV from a 2D array?
by merlyn (Sage) on Dec 14, 2000 at 05:34 UTC

      Text::CSV_XS is faster.

      --
      <http://www.dave.org.uk>

      "Perl makes the fun jobs fun
      and the boring jobs bearable" - me

Re: How do I create a CSV from a 2D array?
by bart (Canon) on Sep 30, 2004 at 15:07 UTC
    If you plan to print it to a file directly, and not quoting the individual fields (for a simple tab separated values table) I think using the special variables $, and $\ is the approach leaving the code most readable:
    @headers = qw(one two three); @rows = ( ['yes', 'no', 'maybe'], ['alpha', 'beta', 'and all the rest'], ); { local($\, $,) = ("\n", "\t"); print @headers; print @$_ foreach @rows; }

    It's more complex if you want a CSV file, where the field values can contain the delimiter, the quoting character, or a newline. The next code will only quote a field if necessary:

    @headers = qw(one two three); @rows = ( ['yes', 'no', 'maybe'], ['17" monitor', 'a;b', "foo\nbar"], ); { local($\, $,) = ("\n", ";"); print map { my $s = $_; $s =~ s/([\n;])|"/ $1 || '""' /ge and $s = qq["$s"]; $s } @$_ foreach \@headers, @rows; }
    Following the modern Microsoft preference, I've used semicolons for the delimiter; and I've doubled the literal quotes appearing in the field data.
Re: How do I create a CSV from a 2D array?
by jdporter (Paladin) on Mar 02, 2011 at 18:30 UTC
Re: How do I create a CSV from a 2D array?
by ichimunki (Priest) on Dec 14, 2000 at 05:24 UTC
    As long as you are sure the list is two dimensional, this should work. First argument is the delimiter, second is the array. If you print the returned scalar to a filehandle, you will create a .csv file:
    -------
    my $write_this_to_file = delimit( ", ", @array ); sub delimit { my $delim = shift; my $delimited_file; foreach $row (@_) { $delimited_file .= join ($delim, @$row); $delimited_file .= "\n"; } return $delimited_file; }
Re: How do I create a CSV from a 2D array?
by fenonn (Chaplain) on Jun 08, 2001 at 01:43 UTC
    As long as @data is a 2D array this statement will store the data in CSV form in the scalar $temp so that it can be printed out to a file.
    $temp = join "\n", map { $_ = join ",", @{$_} } @data;
      This doesn't account for field values which may contain double-quotes, single-quotes, commas, or embedded newlines.
Re: How do I create a CSV from a 2D array?
by Anonymous Monk on Dec 18, 2000 at 22:08 UTC

    or, alternately:

    sub delimit { my $delim = shift; return join('', map { join($delim, @$_) . "\n" } @_); }

Re: How do I create a CSV
by I0 (Priest) on Dec 14, 2000 at 20:30 UTC
    Do you mean like this?  print join "\t",map{join ",",@$_}([1,2,3],["abc","def"]);

    Originally posted as a Categorized Answer.

Re: How do I create a CSV
by AgentM (Curate) on Dec 14, 2000 at 04:36 UTC
    What's the output format type? HTML? Terminal? For HTML, your best bet are tables or lists. For the terminal, you might try using join or using nested for loops to print out "The Matrix" to STDOUT. Since I can't give any info on the output you want, I cannot be more specific. If you want pretty output to the terminal, Curses is your best bet.

    Originally posted as a Categorized Answer.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://46529]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2024-04-19 01:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found