Hi everyone, I am tried to parse a CSV file. The problem i 'm having is that certain fields of that file have commas inside them as well as carriage return characters. Now i tried using CSV_XS and setting as follows
$csv = Text::CSV_XS->new({binary => 1});
This does not seem to have any effect and returns broken records. The following is an example of what i did:
$csv = Text::CSV_XS->new({binary => 1}); while (<INFILE>) { #while ($csv->parse($_)) if (/"X"/) { #The trash data has these 3 characters in it print "THERE IS TRASH IN THE FILE:: $_\n"; } elsif ($first_line==1){ $first_line=0; } else { #Now to deal with the data I want to keep if($csv->parse($_)) { #checks to see if data exists in + $_ and +parses it if it does my @fields=$csv->fields; # puts the values from each field in + an +array my $elements=@fields; #gets the number of elements in the +arra +y my $x=0; # for ($x=0;$x<$elements;$x++) { # print "$fields[$x]\t"; print "\n"; if ($fields[$x]=~ m/\d+/i){ print "LOGID: $fields[$x++]\n"; print "LOGDATE: $fields[$x++]\n"; print "EMPID: $fields[$x++]\n"; print "CATEGORY: $fields[$x++]\n"; print "SUBCAT: $fields[$x++]\n"; print "OS: $fields[$x++]\n"; print "DESCR: $fields[$x++]\n"; print "ACTION: $fields[$x++]\n"; print "ASSIGNTO: $fields[$x++]\n"; print "STATUS: $fields[$x++]\n"; } else { print "\problematic field found "; $counterp++; print "LOGID: $fields[$x++]\n"; # $asdf=<>; } # } $counter++; } } }
I am not sure what i am doing wrong but it probably has to do with While <INFILE>, I would appreciate any help i could get

In reply to Text::CSV_XS and "binary" mode by bittis

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.