Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^3: Multiple double quotes within csv

by NetWallah (Canon)
on May 13, 2017 at 00:46 UTC ( [id://1190175]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Multiple double quotes within csv
in thread Multiple double quotes within csv

Your code does not compile.

Here is some Working code, and the output it produces:

#!/usr/bin/perl use strict; use warnings; use Text::CSV; my $csv = Text::CSV->new({ sep_char => ',', quote_char => undef , esc +ape_char=>undef }); while (my $line = <DATA>){ if ($csv->parse($line)) { my @fields = $csv->fields(); print "$fields[0],"; print "$fields[1],"; print "$fields[2]\n"; } else { warn "Line could not be parsed: '$line'\n"; my ($cde, $str, $pos, $rec, $fld) = $csv->error_diag (); print "DIAG:(CDE=$cde, STR=$str, POS=$pos, REC=$rec, FLD=$fld)\n +" } } __DATA__ 0,""Rat Control" <sip:+15559999999@192.168 .5.233>;tag=gK004bb052",9
>perl test2.pl 0,""Rat Control" <sip:+15559999999@192.168 .5.233>;tag=gK004bb052",9
The quotes at the start of Rat Control are problematic, and produce this error on default settings:
DIAG:(CDE=2023, STR=EIQ - QUO character not allowed, POS=4, REC=1, FLD=2)

        ...Disinformation is not as good as datinformation.               Don't document the program; program the document.

Replies are listed 'Best First'.
Re^4: Multiple double quotes within csv
by ww (Archbishop) on May 13, 2017 at 14:41 UTC

    Re the good Abbot NetWallah's observation that your code "doesn't compile" -- spot on and ++ even though his code could still encounter problems with slight variation in the non-conformity of the CSV (discussion below) -- here's why ...and how to fix compilation failure part of the problem:

    #!/usr/bin/perl use strict; use warnings; # OP's code from question at #1190163 use Text::CSV; my $csv = Text::CSV->new({ sep_char => ',' }); while (my $line = <DATA>) { # added open curly if ($csv->parse($line)) { (my @fields) = $csv->fields(); # enclosed my @fields in () s +o $csv does not mask earlier print print "$fields[0],"; print "$fields[1],"; print "$fields[2]\n"; } else { warn "Line could not be parsed: $line\n"; } } __DATA__ 0,""Rat Control" <sip:+15559999999@192.168 .5.233>;tag=gK004bb052",9

    If what you posted -- complete with strict and warnings -- that your attempt to run your code should have at least hinted at what was wrong. Adding use diagnostics (or perhaps use diagnostics -verbose or filtering your program thru splain would have at least allowed you to post code without errors... something the Monks hold to be an indicator that you're serious about learning rather than merely using us for human debuggers.

    As to the underlying problems, attend carefully to Marshall's thorough examination and exposition ... and join him in thanks to Tux for the module.

Re^4: Multiple double quotes within csv
by Anonymous Monk on May 13, 2017 at 07:22 UTC

    Don't set quote_char and escape_char to undef, as that will cause any field that contains a sep_char to beak your data. For the question at hand, options like allow_loose_quotes and allow_loose_escapes are usually the way to go. In ths particular cae, setting escape_char to undef might work, but never set quote_char to undef to "fix" these kind of situations.

      Thank you (++)> I wasn't familiar with Text::CSV, and unaware of "allow_loose_quotes".
      This site is always a source of enlightenment! It has kept me returning for over 12 years.

              ...Disinformation is not as good as datinformation.               Don't document the program; program the document.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1190175]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (6)
As of 2024-04-19 08:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found