Thanks all for your valuable suggestions and feedbacks.
Now, here is another requirement, i have the file of format as below,(from a sql query output)
BED001 |137CM BUDGET BLUE B/SET |BED001 |BASE & MATTRESS | |E
+a |0 |0 |39.47 |0.00
BED0015 |183CM BODY CUSHION BASE SET |BED0015 |BASE & MATTRESS
+ | |Ea |0 |0 |1,099.91 |0.00
The expected output is
BED001,137CM BUDGET BLUE B/SET,BED001,BASE & MATTRESS,,Ea,0,0,39.47,0.
+00
BED0015,183CM BODY CUSHION BASE SET,BED0015,BASE & MATTRESS,,Ea,0,0,"1
+,099.91",0.00
The code i tried is
use strict;
use Text::CSV;
my $csv = Text::CSV->new( { sep_char => "|", allow_whitespace => 1
+ } );
my $infile = "s.txt";
my $outfile = "s.csv";
open( my $fh, "<", $infile );
open( my $out, ">", $outfile );
while ( my $array = $csv->getline($fh) ) {
local $" = ",";
local $\ = "\n";
#$csv->combine(@{$array});
#my $line=$csv->string();
print $out "@{$array}";
#print $out $line;
}
The actual output:
BED001,137CM BUDGET BLUE B/SET,BED001,BASE & MATTRESS,,Ea,0,0,39.47,0.
+00
BED0015,183CM BODY CUSHION BASE SET,BED0015,BASE & MATTRESS,,Ea,0,0,1,
+099.91,0.00
I need to quote 1,099.91 in the second line of the output csv since the string contains sep_char ",".Is there any method to define output sep_char ?? or How to achieve the expected result??
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.