cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Browse apps to extend the software in the new JMP Marketplace
Choose Language Hide Translation Bar
ABI
ABI
Level III

JSL: treat variable within "<< set formula" Formula as constant/literal

I feel like there should be an easy way to do this but it is eluding me...

 

Example:

blockSize = 15;
col<<set formula(Sequence(1,1000,1,blockSize));

 

After running script, view formula in datatable -- "blockSize" is still being used as the fourth input for Sequence().

 

I don't really want this, as I'm going to dynamically define blockSize inside a loop that changes for each column it iterates through. How do I get the datatable's formula to be "Sequence(1,1000,1,15)" instead of "Sequence(1,1000,1, blockSize)"?

 

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions

Re: JSL: treat variable within "<< set formula" Formula as constant/literal

blockSize = 15;

Eval(
	Substitute(
		Expr( col << Set Formula( Sequence( 1, 1000, 1, bbb ) ) ),
		Expr( bbb ),
		blockSize
	)
);

View solution in original post

3 REPLIES 3

Re: JSL: treat variable within "<< set formula" Formula as constant/literal

blockSize = 15;

Eval(
	Substitute(
		Expr( col << Set Formula( Sequence( 1, 1000, 1, bbb ) ) ),
		Expr( bbb ),
		blockSize
	)
);
gzmorgan0
Super User (Alumni)

Re: JSL: treat variable within "<< set formula" Formula as constant/literal

 Somes I like to use command strings with variable replacement, especially when I need to use the same command more than once, and if the formula or expression has more than one variable.

dt = New Table( "Testit",
	Add Rows( 1500 ),
	New Column( "bbb", Numeric, "Continuous", Format( "Best", 12 ), ),
	New Column( "ccc", Numeric, "Continuous", Format( "Best", 12 ), ), 

);

cmdStr = "^curcol^ << set formula(Sequence(1,1000,1,^blocksize^));";

curcol = ":bbb";
blocksize = 15;
eval(parse(evalInsert(cmdStr)));
// Eval Insert substitutes the variable values delimited by ^
// Parse converts the string to an expression
// Eval runs the expression
// uncomment the next line to see results of evalInsert  
// show(evalInsert(cmdStr) );

//==can reuse===
curcol =":ccc" ;
blocksize = 5;
eval(parse(evalInsert(cmdStr)));

 

 

 

Craige_Hales
Super User

Re: JSL: treat variable within "<< set formula" Formula as constant/literal

One more approach using the eval(evalexpr( ... expr( x ) ... )) pattern

dt=newtable("example", newcolumn("col"));
blockSize = 15;
eval(evalexpr(col<<set formula(Sequence(1,1000,1,expr(blockSize)))));

Formula with blocksize value substituted for the variableFormula with blocksize value substituted for the variable

The problem all of these solutions are solving is pre-evaluating part of the expression, but not all of it.

EvalExpr() is a function that partially evaluates its argument. The parts that you want evaluated are inside Expr() functions. (Thus the name, EvalExpr.) The result is an expression, which needs to be evaluated to perform the setFormula operation.

Craige