Subscribe Bookmark RSS Feed

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

Highlighted
Barling

Occasional Contributor

Joined:

Jun 23, 2016

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
markbailey

Staff

Joined:

Jun 23, 2011

Solution
blockSize = 15;

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

Staff

Joined:

Jun 23, 2011

Solution
blockSize = 15;

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

New Contributor

Joined:

Jul 25, 2016

 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

Staff

Joined:

Mar 21, 2013

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