Subscribe Bookmark RSS Feed

Re: Parse String with "* " Symbol in it without Creating Additional Spaces

ngodfrey

Community Trekker

Joined:

Nov 20, 2015

Hello,

 

I am programmatically iterating through a couple of arrays to create a string variable that can be parsed and evaluated to Fit a Model to the data automatically.

 

The problem I am running into is the column headers are named "DL* 15".

Therefore, when I attempt to parse and evaluate ":DL* 15" I get an error because the parse creates :DL * 15. The additional space invalidates the evaluation, because the column is named :DL* 15 not :DL * 15.

 

An example of what is going on is shown below:

Capture.PNG

Is there a way to prevent the spacing from occuring when parsing "DL * 15"?

 

Thank you,
Nate

3 ACCEPTED SOLUTIONS

Accepted Solutions
Craige_Hales

Staff

Joined:

Mar 21, 2013

Solution

Use the name() wrapper around the problematic name.

a="D";
b="L";
c="* 15";
f = "name(\!""||a||b||c||"\!")";

ex = parse(f);

show(ex);

ex = Name("DL* 15");

the wrapper isn't really there; it is a hint to the parser that this name does not follow the usual rules. It is also displayed with the wrapper for the same reason. Also notice the \!" to append the quotation marks.

Craige
ngodfrey

Community Trekker

Joined:

Nov 20, 2015

Solution

I said it was solved a little too early.

 

I tried the code but it's not working in the more complex context of my situation where I am iterating through two arrays.

 

Without writing all the code out, this won't work since the word "name" will still show up.

Capture2.PNG

 

After the loop, I will take the final string and first parse it and then evaluate it.

eval(parse(fitmodelstring));

Note, the entire looped string is not shown above.

 

Thank you,
Nate

Highlighted
Craige_Hales

Staff

Joined:

Mar 21, 2013

Solution

the name wrapper goes around the entire name, not just the character that causes the name to look like an expression.

 

Or...

 

There are other ways to build expressions like this, using the expression handling functions to manipulate expressions directly rather than working with strings and parsing the strings into expressions.  If your table had a column with the name "sin(a)*cos(b)" you could do this

ex = expr(
Fit Model(
Y( YVARIABLEPLACEHOLDER ),
Effects( :age, :sex, :height ),
Personality( "Standard Least Squares" ),
Emphasis( "Effect Leverage" ),
Run(
:weight << {Plot Actual by Predicted( 1 ), Plot Regression( 0 ),
Plot Residual by Predicted( 1 ), Plot Effect Leverage( 1 )}
)
)
);
substituteInto( ex, expr(YVARIABLEPLACEHOLDER), asName("sin(a)"||"*"||"cos(b)") );
show(ex);

ex = Fit Model(Y(Name("sin(a)*cos(b)")), Effects(:age, :sex, :height), Personality("Standard Least Squares"), Emphasis("Effect Leverage"), Run(:weight << {Plot Actual by Predicted(1), Plot Regression(0), Plot Residual by Predicted(1), Plot Effect Leverage(1)}));

SubstituteInto searches the first argument for the second argument and replaces it with the third argument.  AsName converts a string to a name. The expr() function is used to pass an expression without evaluating it first.

You'll see the Name() wrapper, a lot, if you name columns like thisYou'll see the Name() wrapper, a lot, if you name columns like this

Craige
5 REPLIES
Craige_Hales

Staff

Joined:

Mar 21, 2013

Solution

Use the name() wrapper around the problematic name.

a="D";
b="L";
c="* 15";
f = "name(\!""||a||b||c||"\!")";

ex = parse(f);

show(ex);

ex = Name("DL* 15");

the wrapper isn't really there; it is a hint to the parser that this name does not follow the usual rules. It is also displayed with the wrapper for the same reason. Also notice the \!" to append the quotation marks.

Craige
ngodfrey

Community Trekker

Joined:

Nov 20, 2015

Solution

I said it was solved a little too early.

 

I tried the code but it's not working in the more complex context of my situation where I am iterating through two arrays.

 

Without writing all the code out, this won't work since the word "name" will still show up.

Capture2.PNG

 

After the loop, I will take the final string and first parse it and then evaluate it.

eval(parse(fitmodelstring));

Note, the entire looped string is not shown above.

 

Thank you,
Nate

pmroz

Super User

Joined:

Jun 23, 2011

You can't mix :D and :name.  

 

Try something like this:

fitmodelstring = fitmodelstring || evalinsert("\[Y(:name("D^a[i]^ * ^b[j]^"), Efffects(]\");

 

 

pmroz

Super User

Joined:

Jun 23, 2011

Some notes on my response:

 

The "\[  ... ]\" construct allows you to put anything inside the double quotes, even more double quotes.

 

evalinsert substitutes values for variables surrounded by the ^ character.

 

 

Highlighted
Craige_Hales

Staff

Joined:

Mar 21, 2013

Solution

the name wrapper goes around the entire name, not just the character that causes the name to look like an expression.

 

Or...

 

There are other ways to build expressions like this, using the expression handling functions to manipulate expressions directly rather than working with strings and parsing the strings into expressions.  If your table had a column with the name "sin(a)*cos(b)" you could do this

ex = expr(
Fit Model(
Y( YVARIABLEPLACEHOLDER ),
Effects( :age, :sex, :height ),
Personality( "Standard Least Squares" ),
Emphasis( "Effect Leverage" ),
Run(
:weight << {Plot Actual by Predicted( 1 ), Plot Regression( 0 ),
Plot Residual by Predicted( 1 ), Plot Effect Leverage( 1 )}
)
)
);
substituteInto( ex, expr(YVARIABLEPLACEHOLDER), asName("sin(a)"||"*"||"cos(b)") );
show(ex);

ex = Fit Model(Y(Name("sin(a)*cos(b)")), Effects(:age, :sex, :height), Personality("Standard Least Squares"), Emphasis("Effect Leverage"), Run(:weight << {Plot Actual by Predicted(1), Plot Regression(0), Plot Residual by Predicted(1), Plot Effect Leverage(1)}));

SubstituteInto searches the first argument for the second argument and replaces it with the third argument.  AsName converts a string to a name. The expr() function is used to pass an expression without evaluating it first.

You'll see the Name() wrapper, a lot, if you name columns like thisYou'll see the Name() wrapper, a lot, if you name columns like this

Craige