var
Overview
The var
statement is used to create or update a variable which can subsequently be referenced by name in the USE script.
Syntax
[public] var
name [ = value]
[public] var
name operator number
[public] encrypt var
name = value
For details on encrypted variables please refer to the encrypt article.
Details
Variables are created in one of two ways:
Manually via the
var
commandAutomatically, as a consequence of other statements in the script
If the word public
precedes a variable declaration then the variable will be shown in, and its value can be updated from, the Exivity GUI. Only variables prefixed with the word public
appear in the GUI (all others are only visible in the script itself). To make an automatic variable public, re-declare it with a value of itself as shown below:
Manually defined variables
A variable is a named value. Once defined, the name can be used in place of the value for the rest of the script. Amongst other things this permits the configuration of various parameters at the top of a script, making configuration changes easier.
The = value portion of the statement is optional, but if used there must be white space on each side of the =
character. To use spaces in a variable value it should be quoted with double quotes.
Once a variable has been defined it can be referenced by prefixing its name with ${
and post-fixing it with a }
. For example, a variable called outputFile
can be referenced using ${outputFile}
. If no value is specified, then the variable will be empty, eg:
will result in the output:
Variable names are case sensitive, therefore ${variableName}
and ${VariableName}
are different variables.
If there is already a variable called name then the var
statement will update the value.
There is no limit to the number of variables that can be created, but any given variable may not have a value longer than 8095 characters.
Arithmetic
Variables that contain a numeric value can have the arithmetic operations performed on them in one of two ways.
Method 1
The first, and recommended way is to use an expression, as demonstrated in the example code below:
When using expressions in this manner after a var
statement it is necessary to enclose the expression in parentheses as shown above but both integer and floating point arithmetic can be performed.
Method 2
If working with integer arithmetic then one of the operators +=
, -=
, *=
, /=
or %=
can be used which will perform addition, subtraction, multiplication, (integer) division or modulo operations respectively.
For example the statement var x += 10
will add 10 to the value of x. Note that both the value in the variable and the value following the operator must be integers.
When performing arithmetic operations on a variable using this second method, any leading zeros in the value of that variable will be respected:
Currently, only integer arithmetic is supported by the +=
, -=
, *=
, /=
and %=
operators.
Automatic variables
Automatic variables are referenced in exactly the same way as manually created ones; the only difference is in the manner of creation.
The following variables are automatically created during the execution of a USE script:
To derive the short versions of the day and month names, use a match statement to extract the first 3 characters as follows:
match day "(...)" ${DAY_NAME_UTC}
var short_day = ${day.RESULT}
The .LENGTH suffix
On occasion it may be useful to determine the length (in characters) of the value of a variable. This can be done by appending the suffix .LENGTH
to the variable name when referencing it. For example, if a variable called result has a value of success then ${result.LENGTH}
will be replaced with 7
(this being the number of characters in the word 'success').
A variable with no value will have a length of 0, therefore using the .LENGTH
suffix can also be used to check for empty variables as follows:
myvar.LENGTH
is not a variable in its own right. The .LENGTH
suffix merely modifies the manner in which the myvar
variable is used.
Examples
Basic variable creation and use
Creating encrypted variables
Last updated