in reply to Parsing CSV file

When you have CSV data, it's best to use a CSV parser:

https://metacpan.org/pod/Text::CSV

There is a good amount of sample code for you right there in the docs. However, your particular error is one of scoping and array index syntax.
#!/usr/bin/env perl use strict; use warnings; use v5.10; use Text::ParseWords; my $file = $ARGV[0] or die "Need a CSV file"; open my $fh, '<:encoding(UTF-8)', $file or die "Oops: $file $!"; while (my $line = <$fh>) { chomp $line; my @fields = quotewords(',',0,$line); for my $i (0..$#fields) { say "$i $fields[$i]"; } }

Please note that the entire code block above is untested and I've never used Text::ParseWords. I simply built an example from your supplied attempt.

Replies are listed 'Best First'.
Re^2: Parsing CSV file
by Joma (Initiate) on Jul 05, 2016 at 14:07 UTC

    I normally use a parser using regular expressions, but decided to try this approach as well. Your solution does work. Thank you