Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Text::CVS_XS help when using different separator

by mlaro (Initiate)
on Mar 03, 2012 at 17:39 UTC ( [id://957698]=perlquestion: print w/replies, xml ) Need Help??

mlaro has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, I don't use perl very often and TEXT::CSV_XS is kicking my ass.

I would use Text::CVS which was working great for me except that it doesn't handle french characters.

I also have to use a different seperator because some field have multiple comma separated characters. This is the data:

374|Mike who|kelticjan|mike@someemail.com|23/2/2012 10:57:32|0|0|Mike| +Clements|90 Street Ave|Halifax|New Brunswick|1t5 7y7| 516- +555-5555 |lol + Transportation Inc.|a||a||||a,b,c,d,e,g,h,i,j,k||a|a|a|a|a,b,c,d,e|I +dle time / miles per gallon|a,b,c||b||a,d,f,g,h|

this is what I am trying to do to parse it and print it out with no separator character.

#!/usr/bin/perl use Text::CSV_XS; my $csv = Text::CSV_XS->new ({ quote_char => '"', escape_char => '"', sep_char => "|", eol => $\, always_quote => 0, quote_space => 1, quote_null => 1, quote_binary => 1, binary => 1, keep_meta_info => 0, allow_loose_quotes => 0, allow_loose_escapes => 0, allow_whitespace => 0, blank_is_undef => 0, empty_is_undef => 0, verbatim => 0, auto_diag => 0, }) or die "Cannot use CSV: ".Text::CSV_XS->error_diag (); open my $FH, "<:encoding(utf8)", "test.csv" or die "test.csv: $!" +; while (my $line = $csv->getline ($FH)){ if ($csv->parse (@$line)) { my @field = $csv->fields; foreach my $col (0 .. $#field) { print $field[$col], "\n" ; } } else { print STDERR "parse () failed on argument: ", $csv->error_input, "\n"; $csv->error_diag (); } }

this is the output:

374

I've tried a bunch of ways and seem to be confused, its probably not just the separator ... I'd appreciate some help.

Thanks Mike

Replies are listed 'Best First'.
Re: Text::CVS_XS help when using different separator
by Tux (Canon) on Mar 03, 2012 at 21:52 UTC

    Overcomplicated code, but the "problem" is that you trie to parse the data twice, as getline already parses

    while (my $line = $csv->getline ($FH)) { if ($csv->parse (@$line)) { my @field = $csv->fields; => while (my $line = $csv->getline ($FH)) { my @field = @$line;

    Enjoy, Have FUN! H.Merijn

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://957698]
Approved by BrowserUk
Front-paged by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-03-29 05:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found