$quoteChar $field $quoteChar $separationChar (no spaces)
So for example:
"Perlmonks", "http://www.perlmonks.org", "excellent ;)"
####
#!/usr/bin/perl -w
use strict;
my $debug = 1;
my $read_file = 'in.csv';
my $write_file = 'out.csv';
my $arrayref = parseCSV($read_file);
for my $line (@{ $arrayref }) {
for my $field (@{ $line }) {
print "Field: $field\n";
}
}
printCSV($write_file, $arrayref);
# parse a csv file into an array of arrays
sub parseCSV {
my $file_path = shift;
my $separationChar = ',';
my $quoteChar = '"';
my $escapeChar = '\\';
my $inField = 1;
my @data;
# read csv file
open DATA, $file_path or die("Couldn't read data file: $!");
while () {
# remove newline
chomp;
# split into single chars
my @chars = split('', $_);
# store previous letter (for escape codes)
my $previous = '';
my @fields;
for my $c (@chars) {
my $dataString;
if (($c eq $quoteChar) && ($previous ne $escapeChar)) {
if ($inField) {
$inField = 0;
next;
} else {
$inField = 1;
next;
}
}
if ($inField) {
# ignore all in-field escape chars
if ($c eq $escapeChar) {
next;
}
# append char to data string
$dataString = $dataString . $c
}
if ((! $inField) and ($c eq $separationChar)) {
push(@fields, $dataString);
}
}
push(@data, \@fields);
}
close DATA;
# return a reference to an AoA
return \@data;
}
# format and print an AoA to a CSV file
sub printCSV {
my $file_path = shift;
my $entries = shift; # AoA ref containing entries
my $separationChar = ',';
my $quoteChar = '"';
my $escapeChar = '\\';
my @data;
for my $entry (@{$entries}) {
my $entryString = '';
for my $field (@{ $entry }) {
# escape all existing $quoteChars
my $escapeQuote = $escapeChar . $quoteChar;
$field = $field =~ s/$quoteChar/$escapeQuote/;
# enclose in quoteChars
$field = $quoteChar . $field . $quoteChar;
debug("Field: $field");
# add on to $entryString
$entryString = $entryString . $separationChar . $field;
debug("Entry String: $entryString");
}
# add a newline on the end
$entryString = $entryString . "\n";
push(@data, $entryString);
}
# write @data to the file
open DATA, ">$file_path" or die("Couldn't open $file_path: $!");
print DATA @data;
close DATA;
return;
}
sub debug {
# write to log file instead of
my $message = shift;
if ($debug) {
print $message, "\n";
}
}
####
Use of uninitialized value in concatenation (.) or string
at parseTest.pl line 16.
and
Use of uninitialized value in substitution (s///)
at parseTest.pl line 113.
####
,"","","",""
,""
,""