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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|