cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Try the Materials Informatics Toolkit, which is designed to easily handle SMILES data. This and other helpful add-ins are available in the JMP® Marketplace
Choose Language Hide Translation Bar
pauldeen
Level VI

Name quote a strange column name

I'm trying to insert a column name into a fit model expression. For that I need to escape strange characters in column names to do that we use the :"name"n convention. So how do I go from a string column name to this format?

colListY = {"Plating Thickness (m\!"\!")"};
//This doesn't work
newcolumnref = eval insert("\[:"^colListY[1]^"n]\");

//Looking for this end result
:"Plating Thickness (m\!"\!")"n

Which I can then insert anywhere, for example:

insert into(expr(Y()), newcolumnref);

Anybody know of a robust way to do this?

1 ACCEPTED SOLUTION

Accepted Solutions
Craige_Hales
Super User

Re: Name quote a strange column name

More notes:

I explicitly referenced the table in the column function. Might make a difference if multiple tables are open.

 

If you are concatenating strings, this might do it; you can always use the :"height"n even if not required (height, for example.)

colreftxt = ":" || Regex( Log Capture( Print( colListY[1] ) ), "^\s*(.*)\s*$", "\1" ) || "n";
write(colreftxt);

:"Plating Thickness (m\!"\!")"n

which might be escaped right. Expression manipulation will probably be easier than parsing text assembled like this.

 

 

Craige

View solution in original post

22 REPLIES 22
txnelson
Super User

Re: Name quote a strange column name

You can use the Column, or As Column functions to provide protection to the complex column name

colListY = {"Plating Thickness (m\!"\!")"};

newcolumnref = eval insert("column(\!"^colListY[1]^\!")");
Jim
Craige_Hales
Super User

Re: Name quote a strange column name

If you can avoid getting the name of the column as a string, and get the column object instead, it might be easier. Below, cols is a list of column objects, not their names.

colListY = {"Plating Thickness (m\!"\!")"};
dt = Open( "$sample_data/big class.jmp" );
dt:age << setname( colListY[1] );
cols = dt << getcolumnnames;// don't use the string keyword
/* {name, "Plating Thickness (m\!"\!")"n, sex, height, weight} */

script = Expr(
	Fit Model(
		Y( :weight ),
		Effects( F1 ),
		Personality( "Standard Least Squares" ),
		Run(
			:weight << {Plot Actual by Predicted( 1 ), Plot Residual by Predicted( 1 ),
			Plot Effect Leverage( 1 )}
		)
	)
);

Substitute Into( script, Expr( F1 ), cols[2] );
Write( Name Expr( script ) );
/*
Fit Model(
	Y( :weight ),
	Effects( "Plating Thickness (m\!"\!")"n ),
	Personality( "Standard Least Squares" ),
	Run(
		:weight << {Plot Actual by Predicted( 1 ), Plot Residual by Predicted( 1 ),
		Plot Effect Leverage( 1 )}
	)
);
*/
Craige
pauldeen
Level VI

Re: Name quote a strange column name

@txnelson The problem is that when building the fit model script the column() format cannot be used:

 

Fit Model(
	//Y( :"Plating Thickness (m\!"\!")"n ), <- works
	Y(column("Plating Thickness (m\!"\!"")), //<- does not work
	Effects(
		:"Shift-temp"n, :Position, :Subgroup, :"Shift-temp"n * :Position,
		:"Shift-temp"n * :Subgroup, :Position * :Subgroup
	),
	Personality( "Standard Least Squares" )
);

@Craige_Hales the users selects the columns with a Col list box() and I pass a << get items message to it. That gives me the list of column names as a string. I could locate those column names in the table and find the column number but when assembling the effects expression this method will not allow me to build the interactions as shown above.

 

Is there a way to do expression concatenation to go from string to col reference with name escape?

 

Craige_Hales
Super User

Re: Name quote a strange column name

try this combination of the two answers:

colListY = {"Plating Thickness (m\!"\!")"};
dt = Open( "$sample_data/big class.jmp" );
dt:age << setname( colListY[1] );

script = Expr(
	Fit Model(
		Y( F1 ),
		Effects( :height ),
		Personality( "Standard Least Squares" ),
		Run( F1 << {Plot Actual by Predicted( 1 ), Plot Residual by Predicted( 1 ), Plot Effect Leverage( 1 )} )
	)
);

Substitute Into( script, Expr( F1 ), Column( dt, colListY[1] ) );
Write( Name Expr( script ) );
/*
Fit Model(
	Y( Column( "Plating Thickness (m\!"\!")" ) ),
	Effects( :height ),
	Personality( "Standard Least Squares" ),
	Run(
		Column( "Plating Thickness (m\!"\!")" ) << {Plot Actual by Predicted( 1 ),
		Plot Residual by Predicted( 1 ), Plot Effect Leverage( 1 )}
	)
)
*/

Eval( script );

run with column substitutedrun with column substituted

Craige
Craige_Hales
Super User

Re: Name quote a strange column name

More notes:

I explicitly referenced the table in the column function. Might make a difference if multiple tables are open.

 

If you are concatenating strings, this might do it; you can always use the :"height"n even if not required (height, for example.)

colreftxt = ":" || Regex( Log Capture( Print( colListY[1] ) ), "^\s*(.*)\s*$", "\1" ) || "n";
write(colreftxt);

:"Plating Thickness (m\!"\!")"n

which might be escaped right. Expression manipulation will probably be easier than parsing text assembled like this.

 

 

Craige
pauldeen
Level VI

Re: Name quote a strange column name

Thanks Craige, It seems like this is more complicated than it should be but it works

 

For anybody trying to do this, I put Craige's solution in a function:

WrapColumns = Function({ColName},{default local},
	ReturnList = {};
	If(is list(ColName),	
		For(i=1, i<=n items(ColName), i++,
			insert into(ReturnList, parse(":" || Regex( Log Capture( Print( ColName[1] ) ), "^\s*(.*)\s*$", "\1" ) || "n");)
		);
	,
		insert into(ReturnList, parse(":" || Regex( Log Capture( Print( ColName ) ), "^\s*(.*)\s*$", "\1" ) || "n");)
	);
	Return(ReturnList);
);
Craige_Hales
Super User

Re: Name quote a strange column name

It is too complicated. You can probably replace the regex() with trim() because that is all it is doing. Originally I thought it was going to remove the outer quotation marks that print() leaves behind. (edit: and there is no need to trim at all for the input to parse!)

 

You might have [1] where you need [i] in your example.

 

Craige
pauldeen
Level VI

Re: Name quote a strange column name

Updated with Craige's feedback, thanks for the 1/i!

WrapColumns = Function({ColName},{default local},
	ReturnList = {};
	If(is list(ColName),	
		For(i=1, i<=n items(ColName), i++,
			insert into(ReturnList, parse(":" || Log Capture( Print( ColName[i] ) ) || "n"))
		);
	,
		insert into(ReturnList, parse(":" || Log Capture( Print( ColName ) ) || "n"))
	);
	Return(ReturnList);
);
Craige_Hales
Super User

Re: Name quote a strange column name

To explain for any one watching: the print function does two things: it escapes the internal quotation marks (and other special cases) and it adds external quotation marks. That produces a valid string, and Paul's code then adds a : in front and an n afterward and parses the result into a JSL column name constant. There might, or might not, be an actual column that the column name constant can attach to when used in a script. The column does not need to exist when Paul's code runs. Without the print() function, I'm not sure what function I'd use to get the escaping done (the rules are too complicated to write my own.) Because the original name contained embedded quotation marks, it needs the escaping before being turned into a column name constant.

If the data table is open, you can use the column() function in much the same way to get a column object that (I'm pretty sure) can be used in all the same places that the column name constant can appear. That column object refers to a real column, at least until the table is closed. Launching a platform with an expired column object or a column name constant that doesn't match a table column will produce an expected error message.

@XanGregg 

Craige