•
|
The Names Default To Here() function. If you have simple scripting needs, this single command might be sufficient. See Names Default To Here.
|
Unqualified names in a script with the Names Default To Here mode turned on are private to that script. However, the names persist as long as the script persists, or as long as objects created by or holding the script are still active. We recommend that all production scripts start with Names Default To Here(1) unless there is a specific reason not to do so. When the script uses an unqualified name in this mode, that name is resolved in the local namespace.
To refer to global variables, scope the name specifically as a global variable (for example, ::global_name). To refer to columns in a data table, scope with name specifically as a data table column (for example, :column_name).
Note: Names Default To Here(1) defines a mode for a particular script. It is not a global function. One script can have this mode turned on, while another script can have it turned off. The default setting is off.
In JMP 8 and earlier, the only method to the insulate scripts was to use lengthy names that were less likely to collide with names in other scripts. Using Names Default To Here(1) makes this technique unnecessary.
Local() creates local scopes only in specific contexts within a script and cannot enclose a longer script with interacting functions, while Names Default To Here(1) creates a local scope for an entire script.
If you have simple scripting needs, Names Default To Here(1) might be sufficient.
The Names Default To Here() function determines how unqualified named variable references are resolved. Explicitly scoping a variable using here:var_name always works, whether Names Default To Here() is on or off. See Scoped Names for details about here and other scopes.
Enabling the Names Default To Here mode associates a scope called Here with an executing script. The Here scope contains all of the unqualified named variables that are created when they are the target of an assignment (as an L-value). In JMP 8 and earlier, these variables normally would have been placed in the Global scope. Using a Here scope keeps variables in multiple executing scripts separate from each other, avoiding name collisions and simplifying the scripting and management of variable name collisions. You can still share information using the Global scope.
Run this example script one line at a time to see how the Names Default To Here() function changes the resolution of variable names.
1.
|
Run the first line to create a global variable named a that holds the value 1.
|
2.
|
Run the second line to turn on the Names Default To Here mode.
|
3.
|
Run the third line to create a new variable named a in the local space that holds the value 5. This line does not change the value assigned to the global variable a.
|
The unqualified a is resolved to here:a. If Names Default To Here() were not on, a would be resolved to the global variable named a.
Note that if you use ::a instead of global:a in the Show() function, your output is a little different:
You have two scripts with the following definitions, and Names Default To Here() is turned off (the default condition) in both scripts.
3.
|
Run only the show(a); line in Script 1. The result is as follows:
|
The log shows a = 3 because variable a is global, and was last modified by Script 2. This is the default behavior in JMP 9 and later, and it is the only possible behavior in JMP 8 and earlier.
4.
|
7.
|
Run only the show(a); line in Script 1. The result is as follows:
|
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 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.
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.
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). 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.
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.
A namespace is a collection of unique names and corresponding values. You can store references to namespaces in variables. Namespace names are global, because JMP has only one namespace map. Namespace references are variables like any other variable that references an object, so they must be unique within their scope or namespace. The members of a namespace are referenced with the : scoping operator, such as my_namespace:x to refer to the object that is named x within the namespace called my_namespace. See User-Defined Namespace Functions for details about creating and managing your own namespaces. Namespaces are especially useful for avoiding name collisions between different scripts.
Creates a new namespace called nsname (a string expression) and returns a reference to the namespace. All arguments are optional.
Nsname is the name of the namespace in the internal global list of namespace names. Nsname can be used as the prefix in a scoped variable. The function returns a reference to the namespace, and can also be used as the prefix in a scoped variable reference. If nsname is absent, the namespace is anonymous and is given a unique name created by JMP. Show Namespace() shows all namespaces and their names, whether assigned or anonymous.
Important: If you already have a namespace named nsname, it is replaced. This behavior means that while you are developing your script, you can make your changes and re-run the script without having to clear or delete your namespace. To avoid unintentional replacement, you can either use anonymous namespaces, or test to see whether a particular namespace already exists:
|
Note: Namespace() returns a reference to a namespace that already exists. It does not create a new namespace.
As Scoped() is the function form of a scoped reference. The function returns a reference to the specified variable in the specified scope.
Shows the contents of all namespaces contained in the list of global namespaces. Namespaces are not visible unless a reference is made to one, using either the New Namespace or Namespace functions.
Note that these messages, as with all message, must be sent to a scriptable object. A namespace name is not a defined scriptable object and cannot be used in a Send operation. However, you can use the name of a namespace in variable references. For example, nsmane::var is equivalent to nsref::var.
Namespace Messages defines the messages that are supported by user-defined namespace references.
Returns 1 or 0, depending on whether var_name exists within the namespace.
|
|
Returns the unevaluated expression that var_name contains in this namespace.
|
|
Locks all variables in the namespace and prevents variables from being added or removed. <<Unlock unlocks all of the namespace’s variables.
|
|
The following are all equivalent references to a variable that is named b in the namespace that is named nsname that has a reference nsref:
•
|
use anonymous names created by the New Namespace function
|
There is also an option for the Include function (New Context) that creates a namespace that the included script runs in. This namespaces is an anonymous namespace and it is independent from the parent script’s namespace. For example,
There are a number of factors in resolving a named variable reference. Namespace References describes the named variable references that are resolved for specific situations.
If the Names Default To Here mode is on, JMP looks for the variable in these locations:
If the Names Default To Here mode is off, JMP looks for the variable in these locations:
|
|
||||||||||
JMP looks for the variable in the Global namespace.
|
JMP creates the variable in the Global namespace.
|
||||||||||
JMP looks for the variable in the encapsulating New Window window namespace.
|
JMP creates the variable in the encapsulating New Window window namespace.
|
||||||||||
JMP looks for the variable in the encapsulating Context Box namespace contained in a New Window window.
|
JMP creates the variable in the encapsulating Context Box namespace contained in a New Window window.
|
These forms existed in JMP 8. In JMP 9 and later, a, :a, and ::a have the same meaning with the Names Default To Here mode turned off.
A qualified named reference uses the : and :: operators to provide specific information about where a referenced variable resides, or where it is created. Examples of qualified named references include the following:
An unqualified named reference provides no explicit information to completely identify where a variable resides or where it is created. No scoping operator (: or ::) is specified in the reference. To change the behavior of JMP when resolving unqualified named variable references, use the Names Default To Here(1) function. For more details about variable name resolution, see the Rules for Name Resolution in JSL Building Blocks.
1.
|
If the variable is followed by a pair of parentheses ( ), look it up as a function.
|
2.
|
If the variable is prefixed by : scope operator or an explicit data table reference, look it up as a data table column or table variable.
|
3.
|
If the variable is prefixed by :: scope operator, look it up as a global variable.
|
4.
|
If the variable is an explicit scope reference (such as group:vowel), look it up in the user-defined group namespace.
|
5.
|
If the variable is in a Local or Parameter function, look it up as a local variable. If it is nested, repeat until a function call boundary is found.
|
6.
|
7.
|
Look the variable up in the current scope and its parent scope. Repeat until the Here scope is encountered.
|
8.
|
Look the variable up as a variable in the Here scope.
|
10.
|
If Names Default to Here(1) is at the top of the script, stop looking. The scope is local.
|
If the variable is preceded by :: scope operator, create and use a global variable.
If Names Default to Here(0) is at the top of the script, create a global variable.