I have a database with a number of fields containing comma separated values. I need to split these fields in Perl, which is straightforward enough except that some of the va
Did anyone say you have to do it in one step? You could slice of values in a loop. Given your example you could use something like this.
use strict;
use warnings;
use 5.010;
my $s = q{recycling, environmental science, interdisciplinary (e.g., consumerism, waste management, chemistry, toxicology, government policy, and ethics), consumer education};
my @parts;
while(1){
my ($elem, $rest) = $s =~ m/^((?:\w|\s)+)(?:,\s*([^\(]*.*))?$/;
if (not $elem) {
say "second approach";
($elem, $rest) = $s =~ m/^(?:((?:\w|\s)+\s*\([^\)]+\)),\s*(.*))$/;
}
$s = $rest;
push @parts, $elem;
last if not $s;
}
use Data::Dumper;
print Dumper \@parts;