Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: CSV file

by Polyglot (Pilgrim)
on May 20, 2009 at 07:11 UTC ( #765133=note: print w/replies, xml ) Need Help??

in reply to CSV file

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;



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

Replies are listed 'Best First'.
Re^2: CSV file
by dorward (Curate) on May 20, 2009 at 14:37 UTC

    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.



        Thanks a ton all of you guys... i will try these and let you know what comes up.. :)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://765133]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2018-06-22 23:57 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (124 votes). Check out past polls.