Scripting in StreamServe > StreamServe scripting language specifics > Variables > Local variables

Local variables
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.
Declaring local variables
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.
The following example illustrates a variable declaration:
{
num i, j, count;
str company, address;
str custno, custname;

}
Valid characters
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.
Reserved keywords that cannot be used as names
and, begin, bool, break, case, continue, default, do, else, 
end, false, for, goto, if, int, not, num, return, str, struct,
switch, true, while
Initialization of local variables
Scalar variables can be declared and initialized at the same time. For example:
num i, j, count = 3;
str name="Tom", company="OpenText";
If you do not initialize a variable, a numeric variable is initialized to 0 and a string variable is initialized to empty string.
Nested blocks
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
1
2
3
Logged "myStr: Hello World! ". The value is passed on from where it is declared and initialized.
4
5
Logged "myStr: Hello Kitty! ".
6
Logged "myStr: Hello World! " again because Block 3 went out of scope.
Returning substrings from local variables
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"
Overriding built-in script functions
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
}
The following example returns the first three characters in the value of the getdate variable (probably not the expected result):
{
str getdate = "20100615";
$date = getdate(1,3); //string "201"
}
Variable declarations in loops
Avoid declaration of variables of any type in loops, and declare the variables in the block containing the loop instead. For example:
{
num i, j = 1;
while (i++ < 10)
log(j, "still in the loop");
}
Examples
Example 5
{
num amount1 = 10, amount2 = 20, total = amount1 + amount2;
log(1, str(total));
}
 
Example 6
begin
str firstName = "Jim", surName = "Morrison";
str name = firstName + " " + surName;
log(1, name);
end
 
Example 7
{
str firstName = &firstName;
str surName = &surname;
str name = firstName + " " + surName;
log(1, name);
}
 
Example 8
{
str name = "Jim Morrison";
str firstName = name(1,3);
str surName = name(5);
log(1, firstName);
log(1, surname);
}
 
OpenText StreamServe 5.6.2 Updated: 2018-01-26