Hello Joma,
I’ll make three observations on the regex code shown:
There’s no point in capturing to $2 if that capture is never used. It would be better to use a non-capturing group here:
while ($line =~ m/"([^"\\]*(?:\\.[^"\\]*)*)",?|([^,]+),?|,/g) { # ^^^ push(@fields, defined($1) ? $1 : $2); # ^^
When testing for definedness, Perl’s // (logical defined-or) operator is useful and elegant:
push @fields, $1 // $2;
If you had use warnings at the head of your script (and you should!), you would get a Use of uninitialized value warning each time you try to print an array element whose value is undef. You can fix this easily by substituting an empty string:
push @fields, $1 // $2 // '';
Update: ++choroba for pointing out that the Branch Reset pattern (perlre#Extended-Patterns) is a more elegant option here.
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
In reply to Re^3: Parsing CSV file
by Athanasius
in thread Parsing CSV file
by Joma
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |