Hi Athanasius, The main reason behind my initial posting was that, when I used regular expression to parse the CSV file with one or more lines as shown in the code below, it always worked fine. I couldn't figure out why the print outside the while loop didn't work when using the Text::ParseWords quotewords.
$file = $ARGV[0] or die "Missing CSV file on the command line\n"; open($text, '<:encoding(UTF-8)', $file) or die "Could not open '$file' + $!\n"; @fields = (); # initialize @fields to be empty while ($line = <$text>) { chomp($line); # remove the newline at the end of the line while ($line =~ m/"([^"\\]*(\\.[^"\\]*)*)",?|([^,]+),?|,/g) { push(@fields, defined($1) ? $1 : $3); # add the matched fie +ld } # push(@fields, undef) if $line =~ m/,?/; # account for an empt +y last field } foreach $field(0..$#fields) { print $field + 1 . " $fields[$field]\n"; } close $file;
I am a newcomer to Perl and I am really enjoying it. Thanks for your help.
In reply to Re^2: Parsing CSV file
by Joma
in thread Parsing CSV file
by Joma
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |