Skip to main content
Version: 25.1

Packing Your Bag – Variables and Other Containers

Variables aren’t the only kind of container in SenseTalk, and they have some unusual characteristics. Before packing the bag for your adventure it’s a good idea to understand just what sort of container it is!

Like many scripting languages, variables in SenseTalk can be used without declaring them first. Unlike most other languages, if a variable is used before a value has been stored in it, it will evaluate as its name…

put Hello --> Hello

… unless it is a predefined variable. Predefined variables like pi or copyrightSign can be thought of like constants, except that they can be changed if you want (i.e. used as ordinary variables). To see all of the predefined variables, run this command (note: the predefinedVariables is a global property that you can modify):

put the predefinedVariables

Variables are not typed. Any variable can contain any type of value, and a given variable can contain different types of values at different times during a run.

SenseTalk tries to accommodate the user. If a string value is stored in a variable, and that variable is then used in an arithmetic operation, SenseTalk will automatically convert the string value to a number if possible and proceed with the operation.

For more information, see Constants and Predefined Variables

Comparisons

Because variables aren't typed, when two values are compared for equality (or inequality), the type of comparison done depends on the type of the values themselves. SenseTalk first checks whether both values are valid numbers. If they are both numbers, a numeric comparison is performed. Usually this is the desired behavior, but occasionally it may not be. To force a text comparison instead of a numeric comparison, specify as text after one or both of the operands. Text comparisons are case-insensitive by default. For more information, see Comparison Operators.

put 7.00 is "007" --> True (a numeric comparison is performed)
put 7.00 is "007" as text --> False

Similarly, to force a date/time comparison instead of a text comparison, specify as date or as time.

put "1927-03-16" is "March 16, 1927" --> False (text comparison)
put "1927-03-16" is "March 16, 1927" as date --> True

Scope

Local variables are in scope within a handler (a script or procedure), and are not declared. Global variables must be declared in one of two ways. They may be declared on a line beginning with the word global followed by comma-separated list of global variable names. This must be done in each handler where the global variable is used, typically at the beginning of the handler. Alternatively, the word global may be used directly before the variable name each time it is used. For more information, see Global Variables.

SenseTalk also has "universal" variables, which are declared and used in the same way as global variables. Global and universal variables are distinct from each other, and the exact difference in meaning is defined by the host environment where SenseTalk is used. In Eggplant, for example, the values of global variables persist across all handlers during a run, but are reset for the next run, while universal variables retain their values from one run to the next during a session.

Variables Are Containers

In SenseTalk, commands that change the value of a variable (e.g. Set, Add, Sort, Replace, etc.) are described as operating on a "container". A variable is the most common type of container, but there are several other types of container that these commands can modify, including an item in a list, a property in a property list, a global property, or a text file. In addition, a text chunk of any container (such as a specific word or line) is also a container. One limitation is that text chunks and files can only contain text values, so operations that would set their value to a non-text type such as a list or property list will result in the string representation of that value. For more information see, Containers.