Specify where a name is to be resolved by using a scope in the form scope:name where scope indicates how to resolve the name. For example, here:name indicates that the name should be resolved locally. Using the Names Default To Here mode, here:name is equivalent to name. The scope instructs how to look up the name.
scope:name
•
|
Scope can be a resolution rule. For example, here:x means that x should be resolved to a name that is local to the script. Global:x means that x should be resolved to a global name.
|
•
|
Scope can be a namespace reference variable. For example. ref:a means that a should be resolved within the namespace that ref refers to.
|
•
|
Scope can be a data table reference to look up names as column names. For example, dt:height means that height should be resolved as a column in the data table that dt references.
|
•
|
Scope can be the name of a namespace that you created. For example, myNamespace:b where myNamespace is a namespace that you created. "myNamespace":b is equivalent. See Namespaces.
|
The following examples demonstrate how to scope columns that contain formulas. In both scripts, x is a global variable, local variable, and column name.
In the first script, the column name x is unscoped. the formula in the second column multiplies the value in column x by 100. In this case, The result is a column with the values 100, 200, and 300.
::x = 5;
New Table( "Test",
New Column( "x", Values( [1, 2, 3] ) ),
New Column( "y", Formula( 100 * x ) ),
);
In the following script, the formula in column y assigns 500 to x and then adds 50 to x. Each cell in the column contains the value 550.
::x = 5;
New Table( "Test",
New Column( "x", Values( [1, 2, 3] ) ),
New Column( "y", Formula( Local( {x = 500}, x + 50 ) ) ),
);
JMP built-in functions. For example, Builtin:Sqrt(). These names are shared throughout the JMP environment.
|
|
Provides a namespace block inside Names Default to Here(1). Use Local Here() to prevent name collisions when multiple scripts are executed from the same root namespace (for example, when a script executes two button scripts that have the same variables). The argument can be any valid JSL expression.
Local( {Default Local}, ) does not always work due to the lifetime of the local block, but Local Here() is persistent across the call.
|
|
This example uses the Window scope to pass information during execution. Explicitly scoping the variables x and y to this window ensures that JMP does not try to scope x and y in other contexts, such as a data table. The variables x and y are created and used solely inside the Window environment. The Window scope is similar to using Local(), but more useful because Local() is limited in the places that it can be used.
New Window( "Example",
window:gx = 20;
window:gy = 50;
Graph Box(
Frame Size( 200, 200 ),
Handle(
window:gx,
window:gy,
Function( {x, y},
window:gx = x;
window:gy = y;
)
);
Circle( {0, 0}, Sqrt( window:gx * window:gx + window:gy * window:gy ) );
);
);
Figure 7.1 Example of Current Window Namespace
This example uses the Here scope to pass information between windows that are created by the same script. Scoping a variable using Here: is not dependent on turning Names Default To Here() on. The Here: scope is always available.
The Launcher window asks the user for two values. Those two values are passed to the Output window, which uses them to graph a function. The Launcher window scopes aBox and bBox to the window: essentially, those two variables (pointers to Number Edit Boxes) exist only in the Launcher window and are not available to the Output window. The values from those two boxes are then copied into variables that are scoped to Here, and so are available to both windows that are produced by this script.
launchWin = New Window( "Launcher",
<<Modal,
V List Box(
Lineup Box( N Col( 2 ), Spacing( 10 ),
Text Box( "a" ),
window:aBox = Number Edit Box( 50 ),
Text Box( "b" ),
window:bBox = Number Edit Box( 20 ),
),
Lineup Box( N Col( 2 ), Spacing( 20 ),
Button Box( "OK",
// copy values before window goes away
here:a = window:aBox << Get;
here:b = window:bBox << Get;
),
Button Box( "Cancel", Throw( 1 ) )
),
)
);
New Window( "Output",
Graph Box( Y Function( here:a + here:b * Sin( x / 30 ), x ) )
);
Figure 7.2 Launcher and Output