Subscribe Bookmark RSS Feed

Double quote string element in list

martin_snogdahl

Occasional Contributor

Joined:

Aug 17, 2016

Hi there

I have a list of strings:

StringList = {"String1", "String2", "String3"};

Using a for-loop I want to create a new list with the entries shown below:

NewStringList = {"\!"String1\!"", "\!"String2\!"", "\!"String3\!""};

The reason is that I want to use the entries of NewStringList in an Eval(Parse())-expression and get a quoted string as the result:

Eval( Parse( NewStringList[1] ) ) = "String1"

There might be a simple command that I am just not aware of... Thank you

3 REPLIES
ms

Super User

Joined:

Jun 23, 2011

Here is one way using Repeat. A For loop works well too but Repeat has the advantage of returning a list directly.

StringList = {"String1", "String2", "String3"};

NewStringList = Repeat(

    i = 0;

    {i++ ; "\!"" || StringList[i] || "\!""};,

    N Items(StringList)

);

Eval( Parse( NewStringList[1] ) );

martin_snogdahl

Occasional Contributor

Joined:

Aug 17, 2016

Works perfectly. Thank you.

pmroz

Super User

Joined:

Jun 23, 2011

Here's another way that uses evalinsert and \[... ]\.  The sequence \[ means don't interpret anything in the rest of the string until you see the closing ]\.

newstringlist = {};

for (i = 1, i <= nitems(stringlist), i++,

    newstringlist[i] = evalinsert("\["^stringlist^"]\");

);

newstringlist;