Subscribe Bookmark RSS Feed

confused over JSL in expression form

hardner

Community Trekker

Joined:

Nov 13, 2012

I want to store a small table in another table so that a script that uses both in the same report can reconstruct the small one whenever it's called for.  This seems a nice solution because then users of the table can also easily open that small, related table of info while only storing one datatable.

I did get the following to work, after much gnashing of teeth over layers of expr() syntax....

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );

dt2 = Open("$SAMPLE_DATA/Abrasion.jmp");

ts=dt<< Get Script();

Close(dt,nosave);

ne=nameexpr(ts);

eval(substitute(expr(dt2<<newscript("BigClass",n)),expr(n),nameexpr(ne)));

//...later...

dt2<<runscript("BigClass");

dtBigClass=currentdatatable();

But, 2 questions...

1) is it really this complicated?  Having used getscript() to get the script is there no simpler way to "put" the script I got into a table?

2)  I'm not thrilled about using currentdatatable() to have the variable dtBigClass point at the table after running the script.  Seems prone to maybe getting the wrong thing in there. Say the script fails or there's some timing issue, it's still doing to assign some table or other to dtBigClass.  The script looks like "New Table("Big Class.. blah, blah" and I'd really like to run " dtBigClass=NewTable("Big Class...blah blah..."  I'd be very happy with a solution where I can "get" the script into the form of a text string that I can edit and run more straightforwardly.

1 ACCEPTED SOLUTION

Accepted Solutions
Solution

Expression manipulating can be frustrating. It's not always easy to know if function arguments are evaluated at runtime or just accepted as written. Typically, when transferring expressions to GUI elements like Column Formulas or Table Scripts it takes some extra effort because the argument is not evaluated. Sometimes it is easier to build a string and then eval(parse()) than figuring out the optimal usage expr(), evalexpr() etc.

1) It can be somewhat simplified. This example show two ways to do it

dt = Open("$SAMPLE_DATA/Big Class.jmp");

dt2 = Open("$SAMPLE_DATA/Abrasion.jmp");

//1. Expression

Eval(Eval Expr(dt2 << newscript("BigClass", dtBigClass = Expr( dt << get script))));

//2. String

Eval(Parse("dt2 << newscript(\!"BigClass2\!", dtBigClass2 =" || Char(dt << getscript) || ")"));

Close(dt, nosave);

dt2 << runscript("BigClass");

dt2 << runscript("BigClass2");

2) You can do the assignment within the script as above, or like below

dtBigClass = dt2 << runscript("BigClass");

3 REPLIES
Craige_Hales

Staff

Joined:

Mar 21, 2013

1:  Almost that complicated.  NewScript expects its argument to be the script, which is often the right answer. Not this time.

2:  I don't like CurrentDatatable for this job either. 


dt = Open( "$SAMPLE_DATA/Big Class.jmp" );


dt2 = Open( "$SAMPLE_DATA/Abrasion.jmp" );


Eval( Eval Expr( dt2 << NewScript( "BigClass", bigclass = Expr( dt << getscript ) ) ) );


substitute is one way, but you can also use EvalExpr to evaluate just the Expr.  The result of EvalExpr is an expression:

8474_evalexpr.PNG

which still needs to be evaluated.  EvalExpr looked for Expr and evaluated it, then returned the original expression with the substitution made.  Looking back to line 3, there is one more Eval done to actually run the NewScript command (but not the assignment to bigclass).

When the script named "BigClass" runs, it assigns the variable bigclass, avoiding the need for CurrentDatatable.

Craige
Solution

Expression manipulating can be frustrating. It's not always easy to know if function arguments are evaluated at runtime or just accepted as written. Typically, when transferring expressions to GUI elements like Column Formulas or Table Scripts it takes some extra effort because the argument is not evaluated. Sometimes it is easier to build a string and then eval(parse()) than figuring out the optimal usage expr(), evalexpr() etc.

1) It can be somewhat simplified. This example show two ways to do it

dt = Open("$SAMPLE_DATA/Big Class.jmp");

dt2 = Open("$SAMPLE_DATA/Abrasion.jmp");

//1. Expression

Eval(Eval Expr(dt2 << newscript("BigClass", dtBigClass = Expr( dt << get script))));

//2. String

Eval(Parse("dt2 << newscript(\!"BigClass2\!", dtBigClass2 =" || Char(dt << getscript) || ")"));

Close(dt, nosave);

dt2 << runscript("BigClass");

dt2 << runscript("BigClass2");

2) You can do the assignment within the script as above, or like below

dtBigClass = dt2 << runscript("BigClass");

hardner

Community Trekker

Joined:

Nov 13, 2012

Thanks!

I appreciate both these answers.  I probably should work harder to understand the eval expr() based ones but I have to admit my go-to method is to eval(parse()) a text string.  Less mind bending plus I can run the string part and examine what I'm building directly.  However,  I couldn't figure out how to get it into a string...


char(dt<<getscript()) seems obvious in retrospect but I got hung up trying to do

ts=dt<<getscript();

and then char(ts)...

...that does not work.

Likewise, your answer to 2 seems super simple but it did not occur to me that would work!.  I'm still a little surprised  - not obvious to me it would return something to put into dtBigClass but it's great.