?
Solved

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

Posted on 2010-08-19
4
Medium Priority
?
1,641 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Six Sigma Control Plans
Suggested Courses

777 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