Comma separated variable files are another format commonly produced by spreadsheet and database programs. CSV files delimit their fields with commas, and wrap textual data in quotation marks, allowing the textual data to contain commas if required:
"Fred","Flintstone",40 "Wilma","Flintstone",36 "Barney","Rubble",38 "Betty","Rubble",34 "Homer","Simpson",45 "Marge","Simpson",39 "Bart","Simpson",11 "Lisa","Simpson",9 |
CSV files are harder to parse than ordinary delimited text files. The best way to parse them is to use the Text::ParseWords module:
#!/usr/bin/perl -w use strict; use Text::ParseWords; open INPUT, "csv.txt" or die "Can't open input file: $!"; while (<INPUT>) { my @fields = quotewords("," 0, $_); } |
The three arguments to the quotewords() routine are:
The delimiter to use
Whether to keep any backslashes that appear in the data (zero for no, one for yes)
A list of lines to parse (in our case, one line at a time)