cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
austinspencer
Level II

Insert contents of variable/expression rather than variable name into New Script

It would be convenient to be able to assign an expression to a variable and then use that variable to insert the expression into a data table script using New Script. 

 

dt = New Table("test dt");
anExpr = Expr(
	Show("Hello world.")
);
dt << New Script("hello world", anExpr);
Show(dt << Get Property("hello world"));

However, when I try this, the script's content is only the variable name.  It works if you run it in this state, but if you save the data table and load it again later, the contents of the variable anExpr are lost and the table script won't work.

Is there a way to insert the contents of the variable into a table script rather than the variable name?  Perhaps it can be done with Substitute, Eval Expr, etc, but I tried quite a few combinations of such methods without success.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Craige_Hales
Super User

Re: Insert contents of variable/expression rather than variable name into New Script

JMP made the decision, a long time ago, to be friendly for the common case of an explicit script to be added. That makes the uncommon case, a script in a variable, a bit harder.

 

dt = New Table("test dt");
anExpr = Expr(
	Show("Hello world.")
);
// <<NewScript assumes you'll be adding a script that should not be executed. In your
// example the script is a single variable name that will be kept for later. Not its content.
// below,
// evalexpr(...) finds expr(...) and replaces it with its value. (it evaluates nameexpr(anExpr) .)
// nameexpr(...) evaluates by returning the expression in anExpr, without evaluating it. (show(...) does not run at this time.)
// eval(...) runs the newScript method with all the changes made.
eval(evalexpr(dt << New Script("hello world", expr(nameexpr(anExpr)))));
Show(dt << Get Property("hello world"));
//
// dt << Get Property("hello world") = Show("Hello world.");
//

 

Craige

View solution in original post

4 REPLIES 4
Craige_Hales
Super User

Re: Insert contents of variable/expression rather than variable name into New Script

JMP made the decision, a long time ago, to be friendly for the common case of an explicit script to be added. That makes the uncommon case, a script in a variable, a bit harder.

 

dt = New Table("test dt");
anExpr = Expr(
	Show("Hello world.")
);
// <<NewScript assumes you'll be adding a script that should not be executed. In your
// example the script is a single variable name that will be kept for later. Not its content.
// below,
// evalexpr(...) finds expr(...) and replaces it with its value. (it evaluates nameexpr(anExpr) .)
// nameexpr(...) evaluates by returning the expression in anExpr, without evaluating it. (show(...) does not run at this time.)
// eval(...) runs the newScript method with all the changes made.
eval(evalexpr(dt << New Script("hello world", expr(nameexpr(anExpr)))));
Show(dt << Get Property("hello world"));
//
// dt << Get Property("hello world") = Show("Hello world.");
//

 

Craige
austinspencer
Level II

Re: Insert contents of variable/expression rather than variable name into New Script

Well, after trying a few more combinations, I found a way that seems like it works.  However, I have no idea how or why.

dt = New Table("test dt");
anExpr = Expr(
	Show("Hello world.")
);
Eval(Eval Expr(
	dt << New Script("hello world", Expr(Name Expr(anExpr)))
));
Show(dt << Get Property("hello world"));

*Credit to this answer for getting me close.

 

If anyone has any insight into this, I'd be curious to know more.

jthi
Super User

Re: Insert contents of variable/expression rather than variable name into New Script

Here is a good starting point Expression Handling Functions: Part I - Unraveling the Expr(), NameExpr(), Eval(), ... Conundrum on JMP expressions

 

Here is also some info Scripting Guide > Programming Methods > Advanced Expressions 

 

JSL Cookbook also might have some topics which include expression handlingRun a Platform without Knowing the Number or Names of Columns and this one includes good hint for success: Expression handling is a difficult topic. The more example you see, the more you should understand.

 

 

-Jarmo

Re: Insert contents of variable/expression rather than variable name into New Script

There are of course a few ways to do this, here are two:

 

Names Default To Here(1);

dt = new table();

//one way
scriptExpr = expr ( print( "hi there" ) );
eval ( substitute ( 
	expr ( dt << new script ("greeting 1", _x_  ) ),
	expr( _x_ ), 
	nameexpr( scriptExpr ) 
) );

// another way
scriptQuote = jslquote ( print ( "hi there" ) );
eval( evalexpr(
	dt << new script ("greeting 2", expr( parse( scriptQuote ) ) ) 
) ) ;

Cheers,

Brady