got a good one-liner for manipulating this scalar?

L

laredotornado

Hi,

If have a scalar that is a list of values, separated by commas,
examples are

$s = "ab,cd,15"
$s = "ci29s"
$s = "!!!,?????,xxxxx,yy,102"

My question is, how do I take the above list and add apostraphes around
each value in the scalar? The aboev examples would then look like

$s = "'ab','cd','15'"
$s = "'ci29s'"
$s = "'!!!','?????','xxxxx','yy','102'"

Thanks for your help, - Dave
 
J

Jürgen Exner

Hi,

If have a scalar that is a list of values, separated by commas,
examples are

$s = "ab,cd,15"
$s = "ci29s"
$s = "!!!,?????,xxxxx,yy,102"

My question is, how do I take the above list and add apostraphes
around each value in the scalar? The aboev examples would then look
like

$s = "'ab','cd','15'"
$s = "'ci29s'"
$s = "'!!!','?????','xxxxx','yy','102'"

$s =~ s/,/','/g;
$s = "'" . $s . "'";

jue
 
D

Dr.Ruud

(e-mail address removed) schreef:
$s = "ab,cd,15"
$s = "ci29s"
$s = "!!!,?????,xxxxx,yy,102"

how do I take the above list and add apostraphes
around each value in the scalar? The aboev examples would then look
like

$s = "'ab','cd','15'"
$s = "'ci29s'"
$s = "'!!!','?????','xxxxx','yy','102'"


I changed your requirement to dquotes:

$ echo "xxxxx,yy,102" | perl -ple 's/([^,]+)/"$1"/g'
"xxxxx","yy","102"

A variant with tab as the separator:

$ echo "xxxxx,yy,102" | perl -ple 's/,/\t/g'
xxxxx yy 102


But why do you need a "one-liner"?
 
X

Xicheng Jia

Hi,

If have a scalar that is a list of values, separated by commas,
examples are

$s = "ab,cd,15"
$s = "ci29s"
$s = "!!!,?????,xxxxx,yy,102"

My question is, how do I take the above list and add apostraphes around
each value in the scalar? The aboev examples would then look like

$s = "'ab','cd','15'"
$s = "'ci29s'"
$s = "'!!!','?????','xxxxx','yy','102'"


echo "xxxxx,yy,102" |
perl -ple 's/(?=\A|$)|(?<=,)|(?=,)/\047/g'
or

echo "xxxxx,yy,102" |
perl -F, -anle '$"="\047,\047"; print "\047@F\047"'

Xicheng
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top