Hello Monks
As subject tells , i needed some workaround for the issue iam facing in creation of CSV file.
iam not using TEXT::CSV_XS module due to issues and other factors in our environments, currently iam creating CSV file manually as below.
#!/usr/bin/perl
use strict;
use warnings;
open my $fh,">","tmp.csv" or die "Unable to open $! \n";
#read the file line by line and delimt with comma
while(<DATA>){
my @array = split /~/;
my $str = join(",",@array); # Split seperated by comma
print $fh "$str";
}
close $fh;
__DATA__
col1~col2~col3~col4~col5
data11~data12~data13~data14~data15
data21~data22~data23~data24~data25
data31~data32~data33~data34~data35
data,data41~data42~data43~data44~data45
data51,data52,data,junk,specialchar,sometingdata53~data54~data55
The data will be seperated by ~ sysmbol, iam going to split it and join it by comma(which is equivalent to replacing ~ with comma).
i have issues when there is a comma in one of the column data as shown in above sample.
i needed your advice on how do i overcome this for timebeing, we are considering to upgrade to TEXT:CSV_XS module sooner.
iam thinking of enclosing each value of array into double quotes to preserve original comma before writing to CSV file, how can i achieve that?
if at all i have enclose the data in double quotes, is it worth to enclose all values into double quotes or only value having special characters to be enclosed?
in either case i need monks advise on how can i proceed further till i implement CPAN module.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.