Variables are names that hold values, which you reference later in scripts. There are two types of variables:
•
|
Global variables are shared among all scripts that you run in a JMP session.
|
•
|
Local variables apply only to the script context in which you define them. They can also be local to only a piece of a script, as with variables local to a particular function.
|
To limit the scope of variables, you can define them in a namespace, which is a collection of variables, functions, and other unique names. JMP has a single global variable namespace that all scripts use by default. When you use a name plainly, without a qualifying syntax, the name is an unscoped variable and therefore in the global namespace.
The Names Default To Here(1); function makes all unscoped variables in the script local to that script and does not affect the global variable namespace. Advanced Scoping and Namespaces provides more details.
Note: The Names Default to Here option is true by default for custom menus and toolbar buttons. A script that runs when you select a custom menu item or click a custom toolbar button does not affect global variables.
You can also create a variable in a specific namespace. In the following example, the x variable is created in the aa namespace:
Preceding local variables with the Local() function is another option. Both a and b are local variables in the following expression:
Scoping operators also distinguish a global variable from a local variable. For more information, see Rules for Name Resolution.
The Show Symbols() function lists all variables and namespaces that are defined both globally and in the local script, along with their current values. Here is an example of Show Symbols() messages that are shown in the log:
Show Symbols();
Tip: The JSL debugger also shows you the values of variables and namespaces. See Debug or Profile Scripts in Scripting Tools for more information.
The Clear Symbols() function erases the values set for variables that are defined both globally and in the local script. For example, after you clear and then show symbols, the variables are empty.
Show Symbols();
Note: The older Show Globals() and Clear Globals() functions are aliases of the newer Show Symbols() and Clear Symbols() functions.
To remove all global variables and namespaces, use the function Delete Symbols(). After the last Show Symbols() in the following script is run, nothing shows up in the log. All variables have been completely removed from memory.
Show Symbols();
To list variables in all namespaces, use Show Namespaces(). To delete only a specific namespace, use ns << Delete. Clear Symbols() and Delete Symbols() do not clear or delete variables in each namespace, although they do clear and delete variables that contain references to namespaces. See Rules for Name Resolution for details about unscoped variables.
Note: Clear Symbols() and Delete Symbols() break all scripts that are currently in use. These functions can be very useful in a programming and debugging environment, but do not include them in any script that you plan to distribute. If you include Names Default To Here(1) in your scripts, clearing and deleting global symbols is unnecessary.
If you want to lock a variable to prevent it from being changed, use the Lock Symbols() function. (Lock Globals() is an alias.)
To release the lock and enable the global to be changed, use the Unlock Symbols() function. (Unlock Globals() is an alias.)
The primary use of these two commands is to prevent inadvertent changes to variables. For example, locking a variable prevents Clear Symbols() from clearing a variable that is being used by another script.