in reply to CSV nightmare
Also, there's another problem with your second solution. Unicode support remains on Text::CSV_XS's todo list.
You could instead try the largely compatible Text::CSV::Unicode.
#!/usr/bin/perl use warnings; use strict; # solution 2, converted to Text::CSV::Unicode # replacement for Text::CSV_XS use Text::CSV::Unicode; my $csv = Text::CSV::Unicode->new(); my $count=0; my $file = 'PS0002_9_2006b.txt'; if (defined $ARGV[0]) { $file = $ARGV[0]; } my $sum = 0; # ikegami's fix open(my $data, '<:raw:encoding(UCS-2le):crlf:utf8', $file) or die "Cou +ld not open '$file'\n"; while (my $line = <$data>) { chomp $line; exit if ($count >= 20); $count++; if ($csv->parse($line)) { my @columns = $csv->fields(); $sum += $columns[2]; } else { warn "Line could not be parsed: $line\n"; } } print "$sum\n"; # end solution 2_______________________
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: CSV nightmare
by Tux (Canon) on Jun 03, 2008 at 08:50 UTC | |
by ikegami (Patriarch) on Jun 03, 2008 at 10:36 UTC | |
by Tux (Canon) on Jun 03, 2008 at 15:10 UTC | |
by ikegami (Patriarch) on Jun 03, 2008 at 19:26 UTC | |
by ikegami (Patriarch) on Jun 04, 2008 at 23:19 UTC | |
by Tux (Canon) on Jun 05, 2008 at 05:33 UTC |
In Section
Seekers of Perl Wisdom