var Command

               var [type] name [= exp], ...
The var command defines variables for later use. The var command consists of an optional type declaration (string, int, float, bool, date, time, timestamp) followed by a comma separated list of variable declarations. Each variable declaration consists of a variable name followed by an optional initialization.

A Jive variable name begins with a letter or underscore followed by zero or more letters, underscores and numeric digits. Variable names can also be quoted using the back quote (`). Back quoted variables can contain Jive keywords, delimiters and spaces.

Variable initialization is optional. It consists of an equal sign (=) followed by an initialization expression. For example,

var int x = 0
If the type declaration is omitted, the type of each variable declared is determined by the initialization expression. If both the type and initialization is omitted, the variable is declared as type bool with value null.

Variables can be redefined within a block. A second declaration of a variable in a block will completely replace the first. The type may be changed in the second declaration.

Arrays

Variable arrays are also possible. To define a variable as an array, follow the variable name with a left bracket ([). The left bracket is followed by an optional expression and a right bracket. The optional expression is converted to integer and used to define the number of elements in the array (which may be zero). If the expression is omitted, the number of elements is determined by the initialization expression.

Array Initialization

The initialization expression (following the equal sign) for an array has three forms - a variable name, an expression or a list. If the variable named is an array, it is used as the initialization array. If an expression is used or the variable is not an array, the value is converted first to string and then converted to a string array with an element for each character in the first string. This string array is used as the initialization array.

If the initialization expression is a list, the elements of the list form the initialization array. The list is enclosed in braces with comma separators. Each list element is an initialization expression. For example,

var string choices[] = {'Vanilla','Chocolate','Strawberry'}
Once the initialization array is determined, it is assigned to the array variable. If the number of elements is not defined, the number of elements in the initialization array is used. The elements of the new variable array are then set one by one from the initialization array. The initialization array is truncated or extended with null elements as needed.

Type conversion is perform if a type was declared. Otherwise, the type of the initialization array element is used to type each element. This allows arrays with elements of mixed type:

var mixed[] = {'text',123,4.567,1996/1/1,true}

Array References

Array variable elements are referenced in expressions using subscripting. The variable name is followed with an integer expression enclosed in brackets. This notation can also be used in the set Command to set the value of array elements. For example,
set array[2] = array[1]
References outside of array bounds are null. Non-array variables may also be subscripted. They are treated like single element arrays. If an array variable is referenced without subscript, the first element is retrieved, as if '[0]' was appended to the name.

Array Operators

Two special operators are available for array variables - @ and #. The # operator is unary and takes an variable name as an operand. Its value is the number of elements in the variable array. If the variable is not an array, the value of # is 1.

The @ is also unary but takes a full initialization expression as an operand. If the expression is an array, each of its elements is converted to string and concatenated together to form a single string. If the expression is not an array, it is simply converted to string.


Copyright © 1996 FFE Software All Rights Reserved WorldWide