|KiXtart 2001 Manual|
Dynamic Program Variables
In KiXtart, variables are used to temporarily store values during the execution of a script. Variables have a name (the word you use to refer to the value the variable contains) a type (which determines the kind of data the variable can store) and a scope (which determines where in the script you can reference the variable). You can think of a variable as a placeholder in memory for an unknown value.
Storing data in variables
Variables can be assigned a particular value by using an assignment statement:
$Variable = 10
or by using a GET or GETS statement :
Optionally, variables can be created and assigned a value on the command line with which KiXtart is started. To do this, type the variable name followed by an equal sign (=) and the value the variable should have. For example:
KIX32 Demo.kix $Key=Value
On the command line, do not include spaces between the equal sign (=) and the value. If you want to specify a value that contains spaces, enclose it in quotation marks (for example, KIX32 Demo.kix $Key="Hi there").
To declare a variable is to tell the program about it in advance. You declare a variable with the Dim or the Global statement, supplying a name for the variable:
Variables declared with the Dim statement exist only as long as the script is executing. When the script finishes, the variable, and its value, disappear. Variables declared with the Global statement exist during the entire KiXtart session.
A variable name:
You can use the same name for variables in different scopes, and if you do, you
will only be able to reference the variable in the current scope. Please see the
example below for more details:
$Var = 10 IF InGroup("Admins") DIM $Var ; local variable with same name $Var = 20 ? $Var ; this will display '20' ENDIF ? $Var ; this will display '10'
Variables don’t have to be declared before they can be used. You can also implicitly declare them simply by assigning a value to them. Note that all variables that are declared in this way have a global scope (see below for details on scope).
Scope of variables
Depending on how and where they are declared, variables can have a local or a global scope. Variables with a global scope are visible anywhere in any script during the entire KiXtart session. Variables with a local scope are only visible to the script or script segment in which they were created.
$GlobalVariable = 10
|Assuming this is the first reference to ‘$GlobalVariable’, this variable is implicitly declared and will become a global variable, visible everywhere in every script during this KiXtart session.|
$LocalVariable = 10
|This variable will become a local variable and will be visible only in the current script.|
IF $X = 1 DIM $LocalVariable $LocalVariable = 10 ENDIF
this example, $LocalVariable will only be visible inside the IF statement.
:Demo DIM $LocalVariable $LocalVariable = 10 RETURN
this example, $LocalVariable will only be visible inside the subroutine
There are two types of variables: string and integer. String variables can contain up to 32,000 characters. Integer variables can contain any value between -2,147,483,648 and 2,147,483,647. The type of a variable is automatically changed to the result of the expression that is assigned to it. This means that if you assign a string to an integer, the integer is changed to a string.
There is no limit on the number of variables that can be defined, other than the amount of memory available to KiXtart.
KiXtart supports single dimension arrays. Arrays allow you to refer to a series of variables by the same name and to use a number (an index) to tell them apart. This helps you create smaller and simpler code in many situations, because you can set up loops that deal efficiently with any number of cases by using the index number. Arrays have both upper and lower bounds, and the elements of the array are contiguous within those bounds. Because KiXtart allocates space for each index number, avoid declaring an array larger than necessary.
Unlike normal variables, arrays must be declared explicitly before they can be used.
When declaring an array, follow the array name by
the upper bound in square brackets. The upper bound cannot exceed 2,147,483,647.
Dim $Counters ; explicit declaration Dim $Sums ; explicit declaration
$NewArray = 10, 20, 30 ; implicit declaration
$NewArray = "A1", "B2", "C3" ; implicit declaration
The first declaration creates an array with 15
elements, with index numbers running from 0 to 14. The second creates an array
with 21 elements, with index numbers running from 0 to 20. The third, implicit,
declaration creates an array with 3 elements, with index numbers running from 0
All elements of an array that has been declared explicitly have the same type: integer. Individual elements of an array can subsequently be changed to the string type (just as any other variable) simply by assigning a string expression to them.
Arrays are always vartype variant.
Note: Unlike regular variables, arrays can not be used inside strings and can also not be assigned a value on the command line.