Hi all I'm having a problem when writing a value which contains a comma from a database to column in a CSV file. I'm able to remove all commas from the value, however I want to keep them and escape them. The $content variable is the value which contains the commas. sorry didn't mention this I'm using Text::CSV
use Text::CSV; open(MYTEXT, ">>/home/ibrahimg/transactionslogs/${client}_Transactions +.csv") || die; my $testQuery = "SELECT serial_num,FROM_UNIXTIME(trans_date) AS 'Actio +n_date', actor, type, trans_data,trans_data_from, content FROM trans +ORDER BY serial_num; "; my $sth1 = $epm_db->Query($permissionQuery); $sth1->execute(); my $serial_num; my $actionDate; my $actor; my $type; my $trans_data; my $content; my @data; while ( my $dat_ref = $sth1->fetchrow_hashref("NAME_lc") ) { push @data, $dat_ref; } print MYTEXT "Serial_number,Action_date,Actor,Type,Trans_data, +Trans_data_from,Content\r";# printing headings foreach my $element (@data) { $serial_num = $element->{'serial_num'}; $actionDate = $element->{'action_date'}; $actor = $element->{'actor'}; $type = $element->{'type'}; $trans_data = $element->{'trans_data'}; $trans_data_from = $element->{'trans_data_from'}; $content = $element->{'content'}; $content =~ s/\n/ /g; $content =~ s/\r//g; #$content =~ s/,//g; # do not want to remove all commas from value ju +st escape them print MYTEXT "$serial_num,$actionDate,$actor,$type,$trans_ +data,$trans_data_from,$content\r "; # printing values into csv output + file }
any help would be appreciate thank you in advance.

In reply to escaping commas in value when writing to a cvs file by ibra

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.