What if there is a field called: "Foo, Bar" ?
Example 7-9. examples/arrays/process_csv_file_module.csv
Foo,Bar ,10,home Orgo,"Morgo, Hapci",7,away Big,Shrek,100,US Small,Fiona,9,tower
Example 7-10. examples/arrays/process_csv_file_module.pl
#!/usr/bin/perl
use strict;
use warnings;
use Text::CSV_XS;
my $csv = Text::CSV_XS->new();
my $file = 'process_csv_file_module.csv';
if (defined $ARGV[0]) {
$file = $ARGV[0];
}
my $sum = 0;
open(my $data, '<', $file) or die "Could not open '$file'\n";
while (my $line = <$data>) {
chomp $line;
if ($csv->parse($line)) {
my @columns = $csv->fields();
$sum += $columns[2];
} else {
warn "Line could not be parsed: $line\n";
}
}
print "$sum\n";
| Prev | Home (Copyright Gabor Szabo) Perl Training Israel | Next |
| process csv file (short version) | Up | Join |