You tried Text::CSV, which is the wrapper over Text::CSV_PP (pure perl) and the fast Text::CSV_XS. They both have the same syntax:
use Text::CSV;
my $csv = Text::CSV->new ({
binary => 1, # allow binary data
auto_diag => 1, # allow automatic warnings and errors
sep_char => ",", # , is the default, ; is also use quite often
});
open my $fh, "<", "file.csv" or die "file.csv: $!";
while (my $row = $csv->getline ($fh)) {
say "The second field is ", $row->[1];
}
If you want a parser WITHIN a parser, just create a second parser
use Text::CSV;
my $csv = Text::CSV->new ({
binary => 1,
auto_diag => 1,
sep_char => ",",
});
my $parser = Text::CSV->new ({
binary => 1,
auto_diag => 1,
sep_char => ";", # sep is ;
});
open my $fh, "<", "file.csv" or die "file.csv: $!";
while (my $row = $csv->getline ($fh)) {
say "The second field is ", $row->[1];
foreach my $f (@$row) {
$parser->parse ($f) or next;
my @subfields = $parse->fields;
}
}
Enjoy, Have FUN! H.Merijn
-
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.