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
hardner
Level V

confused over JSL in expression form

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
ms
Super User (Alumni) ms
Super User (Alumni)

Re: confused over JSL in expression form

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");

View solution in original post

3 REPLIES 3
Craige_Hales
Super User

Re: confused over JSL in expression form

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
ms
Super User (Alumni) ms
Super User (Alumni)

Re: confused over JSL in expression form

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
Level V

Re: confused over JSL in expression form

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.