Response: I'd go the Text::CSV route, but this might help get you started
use strict;
sub main{
my $text = qq{a,b,"hey, you","str1, str2, str3",end};
print "Input: $text\n\n";
# Split the delimiters
my @values = split( /(?:\,|(\".*?\"))/ , $text);
# Remove the created blanks
@values = grep{$_ ne ''} @values;
# Output
foreach (0..$#values){
print "$_: $values[$_] \n";
}
}
main();
Output:
Input: a,b,"hey, you","str1, str2, str3",end
0: a
1: b
2: "hey, you"
3: "str1, str2, str3"
4: end
Thoughts: I haven't really thought why the blanks are being created - if you take away the grep, you'll see what I'm talking about. I still advise using Text::CSV because using this grep method will remove wanted blanks. Therefore, the above code has structural integrity problems.
Example: a,b,,d,e
You probably really want that space holder there if you're going to be inserting this into a database. The grep would remove it because it has a blank string value ("").
Demize
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.