I dont know if there is a way to do this with split. If
there is I hope someone will post.
Since you are parsing on ',' I think the easiest way is
to let perl handle it naturally with an eval statement.
Since perl would normally intrepret (a, "b,c", d) as an array of three
elements (what we are looking for in this case) just let it do its thing.
$str = 'a, "b,c", d';
@array = eval('('.$str.')');
One obvious problem is that is will throw away white space
unless it is contained in "". But you could probably fix that if you wanted. I dont know how robust this solution is, but it works for
your example. It should be fairly stable though, as long as you
catch the eval errors.
#!/usr/bin/perl
#set string to split on ","
my $str = 'a,"b,c",d';
#use perl to parse an array as it normally would
#
# after the eval this next line will look like:
# my @a = (a,"b,c",d);
#
# so $a[0] = 'a', $a[1] = 'b,c', and $a[2] = 'd'
my @array = eval("(".$str.")");
#catch errors from eval
if( $@ )
{
die "eval error: $@\n";
}
#print out the results.
foreach (@array)
{
print $_, "\n";
}
|