Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: CSV String Split

by Kenosis (Priest)
on Nov 17, 2012 at 20:14 UTC ( #1004347=note: print w/ replies, xml ) Need Help??


in reply to CSV String Split

afoken is correct about using a Module to parse csv data, and your particular data supports that choice, because of the embedded commas within quoted fields. Here's an example using Text::CSV on your data, including a header row--in case you need to handle that (omit, otherwise):

use strict; use warnings; use Text::CSV; my $csvData = <<END; col0,col1,col2,col3,col4,col5,col6,col7 ,"value",,"value1,value2","anothervalue","oh,comeon",,"Givemeabreak" END my $csv = Text::CSV_XS->new( { binary => 1, auto_diag => 2 } ) or die "Cannot use CSV: " . Text::CSV->error_diag(); # Open the string like a file for reading open my $csvfh, '<', \$csvData or die $!; # Get first line (array reference to parsed column names) my $columnNames = $csv->getline($csvfh); # $row contains an array reference to the parsed csv line while ( my $row = $csv->getline($csvfh) ) { print "Col$_: $row->[$_]\n" for 0 .. 7; } close $csvfh;

Output:

Col0: Col1: value Col2: Col3: value1,value2 Col4: anothervalue Col5: oh,comeon Col6: Col7: Givemeabreak

An array reference to the parsed csv line's data is returned to $row which is later dereferenced using the arrow operator; e.g., $row->[0] contains the value of column zero.

Hope this helps!


Comment on Re: CSV String Split
Select or Download Code
Re^2: CSV String Split
by jcleland (Acolyte) on Nov 17, 2012 at 20:38 UTC
    Unfortunately:
    Checking if your kit is complete... Looks good Bareword found where operator expected at ./Makefile.PL line 1, near " +30 mtime" (Missing operator before mtime?) Number found where operator expected at ./Makefile.PL line 2, near "27 +" (Missing semicolon on previous line?) Bareword found where operator expected at ./Makefile.PL line 2, near " +27 atime" (Missing operator before atime?) Number found where operator expected at ./Makefile.PL line 3, near "30 +" (Missing semicolon on previous line?) Bareword found where operator expected at ./Makefile.PL line 3, near " +30 ctime" (Missing operator before ctime?) ERROR from evaluation of /usr/share/cpan/build/HMBRAND-iipV8I/Text-CSV +_XS-0.92/M akefile.PL: ERROR from evaluation of /usr/share/cpan/build/HMBRAND-iip +V8I/Text-C SV_XS-0.92/PaxHeaders.23664/Makefile.PL: syntax error at ./Makefile.PL + line 1, n ear "30 mtime"

    Not sure what to make of that, but the module doesn't want to install. tried cpan <module> and manual build as well. Puke.
      NM, missing modules. Fixed. Thanks for the help and examples guys!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1004347]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2014-07-11 04:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (218 votes), past polls