Solved

perl, using parse_line to delimit by comma, but keep the double-quotes

Posted on 2010-08-19
4
1,514 Views
Last Modified: 2012-05-10
I use parse_line to split a string based on comma (I don't have CSV library and do not want to install it), but I want to keep the double quote.  

use Text::ParseWords;

my $mystring = "one, two, \"three\", four";
my $delim_char = ",";
my $delim_string = "\\s*$delim_char\\s*";
my @fields = &parse_line($delim_char,0,$mystring);
print join("\n",@fields),"\n";
0
Comment
Question by:rgbcof
  • 2
4 Comments
 
LVL 10

Expert Comment

by:jeromee
ID: 33478926
Please take a look at :
http://perldoc.perl.org/perlfaq4.html#How-can-I-split-a-[character]-delimited-string-except-when-inside-[character]%3f


Good luck!
0
 
LVL 8

Accepted Solution

by:
ropenner earned 125 total points
ID: 33480020
Put a 1 for the $keep variable (2nd parameter)

man Text::ParseWords explains the syntax of the below

my @fields = &parse_line($delim_char,1,$mystring);
0
 
LVL 8

Expert Comment

by:ropenner
ID: 33480034
one
 two
 "three"
 four


This is the output if you put the '1' in the code as explained above.  Have a good one.
0
 

Author Closing Comment

by:rgbcof
ID: 33480506
Right on.  Thanks
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now