Local variables are used in scripts and function files. In contrast to global variables (lifetime within the execution of a job and accessible from all script contexts), local variables have a scope and lifetime limited to the block where it is declared. A block, also known as compound statement, is a set of statements contained within curly brackets
{…} or within
begin…end.
Local variables must be declared before they can be used, and the declaration must be done at the beginning of a block before any other statements. In contrast to global variables, local variables do not have a
$-prefix. Local variables must also be assigned a specific type. Local variables are declared as follows:
Type Name-list;
•
|
The Type specifies the type of the variables and can be either num or str. Type num corresponds to a numeric value, as in num($var) for global variables, and must only be assigned numeric expressions. Type str corresponds to a string value and behaves like a global variable except from its scope and lifetime. Note that you cannot apply operations like ++ to a local str variable as you can do to global variables.
|
•
|
The Name-list is a comma separated list of variable names.
|
{
num i, j, count;
str company, address;
str custno, custname;
…
}
A variable name can contain letters (A-
Z,
a-
z), digits (
0-
9) and underscore (
_). Note that a name is
not case sensitive, but it must begin with a letter or underscore. A name can also be followed by one or more dimension specifications within square brackets (
[]) to declare an array variable.
and, begin, bool, break, case, continue, default, do, else,
end, false, for, goto, if, int, not, num, return, str, struct,
switch, true, while
num i, j, count = 3;
str name="Tom", company="OpenText";
Blocks in a script can be nested. A local variable can be passed to any sub-blocks within the block where it is declared. It is possible to re-initialize or re-declare a local variable in a sub-block, but the variable will retain its previous state when the sub-blocks goes out of scope. The example below illustrates this (comment numbers explained below example).
Script 1 "Event" //1
str myStr = "Hello World!"; //2
{ //Block 1
{ //Block 2 (nested)
log(0, "myStr: " + myStr); //3
}
}
{ //Block 3 (not nested)
str myStr = "Hello Kitty!"; //4
log(0, "myStr: " + myStr); //5
}
log(0, "myStr: " + myStr); //6
End
3
|
Logged "myStr: Hello World! ". The value is passed on from where it is declared and initialized.
|
5
|
Logged "myStr: Hello Kitty! ".
|
6
|
Logged "myStr: Hello World! " again because Block 3 went out of scope.
|
You can reference a substring in a local variable by using name(num1[,num2]) where
num1 determines the position of the first character and
num2 determines how many characters from that position to return. For example:
str name="Jim Morrison"
str firstname=name(1,3) //substring "Jim"
If num2 is omitted, the rest of the string is returned. For example:
str name="Jim Morrison"
str surname=name(5) //substring "Morrison"
If you, for example, declare a local variable as str getdate; you will override the built-in script function
getdate(). This means the script function is not available during the scope of the local variable.
The following example will cause a syntax error because the parenthesizes at the end of a variable name is reserved for retrieving a substring from the variable value:
{
str getdate = "20100615";
$date = getdate(); //syntax error
}
{
str getdate = "20100615";
$date = getdate(1,3); //string "201"
}
{
num i, j = 1;
while (i++ < 10)
log(j, "still in the loop");
}
{
num amount1 = 10, amount2 = 20, total = amount1 + amount2;
log(1, str(total));
}
begin
str firstName = "Jim", surName = "Morrison";
str name = firstName + " " + surName;
log(1, name);
end
{
str firstName = &firstName;
str surName = &surname;
str name = firstName + " " + surName;
log(1, name);
}
{
str name = "Jim Morrison";
str firstName = name(1,3);
str surName = name(5);
log(1, firstName);
log(1, surname);
}