I feel like there should be an easy way to do this but it is eluding me...
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)"?
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)));
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)))));
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.