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

Hi, Can anyone suggest me how can i create a csv file using perl. I have four arrays, i need to copy the contents from the four arrays and store in the csv file one after another for example: I need to take the first elements from each array and put it as the first line in the csv file, then the second element of each array and so on... Also please let me know how to redirect the contents of the array to a text file otherwise

Replies are listed 'Best First'.
Re: CSV file
by Tux (Canon) on May 20, 2009 at 06:26 UTC
Re: CSV file
by moritz (Cardinal) on May 20, 2009 at 06:27 UTC
Re: CSV file
by Polyglot (Chaplain) on May 20, 2009 at 07:11 UTC
    For such a simple task, I'm not sure you really need an external module. You could easily do everything you said above like this:
    my @combined = (); my ($col1, $col2, $col3, $col4) = ''; while (@array1) { $col1 = shift @array1; chomp $col1; $col1=~s/,/\,/g; #escape commas $col2 = shift @array2; chomp $col2; $col2=~s/,/\,/g; $col3 = shift @array3; chomp $col3; $col3=~s/,/\,/g; $col4 = shift @array4; chomp $col4; $col4=~s/,/\,/g; push @combined, "$col1,$col2,$col3,$col4\n"; } open OUTPUT, ">$target_file" or die "Cannot open target file! $! \n\n +"; print OUTPUT @combined; close OUTPUT;

    Blessings,

    ~Polyglot~

    UPDATE: Edited for more correct CSV style...I was thinking of "columns" instead of "commas" for some reason.

      Does \, work in CSV? I thought you have to quote bits of data that included commas? Even if a simple escape like that is OK, what if the data includes \ characters? Or new lines?

      Simple data formats very rarely are. Use the modules, they are robust.

        For the record there is currently no such thing as a standard CSV format.

        If you are using Microsoft Excel, it will encapsulate each "cell" of data in quotes, instead of escaping the commas. However, what if the cells have quote marks? I suppose they would need to be escaped if using that format. I found the following "guide" to CSV online.


        Considerations When Exporting CSV

        The biggest differences are in how these three characters are handled.
        • Embedded double quotes in fields.
          An escape character is sometimes used to introduce a double quote, or in place of it.
        • Embedded line-feeds in fields.
          This one is also escaped sometimes. Often like in C ("\n")
        • Embedded commas in fields.
          Again, an escape character is sometimes used in place of the comma
        Theoretically, if you could avoid exporting any fields that include these characters you would greatly improve the chances that your CSV file could be read by variations.

        Is it worth reducing your application's functionality or convenience? No. But if the existence of these characters is entirely superfluous, you might consider taking them out.


        Take your pick. I picked one format. There are others.

        Blessings,

        ~Polyglot~

Re: CSV file
by bichonfrise74 (Vicar) on May 20, 2009 at 16:00 UTC
    Unless I misunderstood your question, this should do the trick...
    #!/usr/bin/perl use strict; my @arrays = qw( a b c d ); open( my $file, "> /tmp/csv_file" ); print $file join ",\n", @arrays; close( $file );