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] varname [ = value]

[public] varname operator number

[public] encrypt varname = value

For details on encrypted variables please refer to the encrypt article

Details

Variables are created in one of two ways:

  1. Manually via the var command

  2. Automatically, 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:

# Convert the automatic NEWLINE variable to be public
public var NEWLINE = ${NEWLINE}

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 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:

var empty_var
print Variable value: "${empty_var}"

will result in the output:

Variable value:

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. This is done using the following syntax:

varname operator number

The operator must be surrounded by white-space and following values are supported:

For example the statement var x += 10 will add 10 to the value of x.

When performing arithmetic operations on a variable, any leading zeros in the value of that variable will be respected:

var x = 000
var x += 5    # Result is 005
var x += 10   # Result is 015
var x += 100  # Result is 115
var x += 1000 # Result is 1115

Attempting to perform an arithmetic operation on a variable that does not contain a valid number will result in an error being logged, and the script will terminate.

Currently, only integer arithmetic is supported.

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:

    var myvar
    if (${myvar.LENGTH} == 0) {
        print The variable 'myvar' is empty
    } else {
        print The variable 'myvar' has a value of ${myvar}
    }

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

    # Declare a variable
    var name = value

    # If the value contains whitespace then it must be quoted or escaped
    var sentence = "This sentence is contained in a variable"

    # Pathnames should be quoted to avoid any incidences of '\t' being expanded to tabs
    var exportfile = "C:\exivity\collected\Azure\customers.csv"

Creating encrypted variables

    # ---- Start Config ----
    encrypt var username = admin
    encrypt var password = topsecret
    var server = "http://localhost"
    var port = 8080
    var api_method = getdetails
    # ---- End Config ----

    set http_authtype basic
    set http_username ${username}
    set http_password ${password}

    buffer {response} = http GET ${server}:${port}/rest/v2/${api_method}

Last updated