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";
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.