Solved

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

Posted on 2010-08-19
4
1,567 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

830 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