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

Local variables
As opposed to global variables, which have a scope within the execution of a job, local variables have a scope that is limited to the compound statement where it is declared. A compound statement is a set of statements contained within curly brackets {}, or within begin/end.
You can use local variables in scripts and function files.
For reasons to use local variables, see Drawbacks of global variables.
No prefix
A local variable does not have the $ prefix.
Declaration
You must declare a local variable before any other statements in the compound statement, and you can not declare a variable more than once within the compound statement.
You declare a local variable with the following syntax:
type name-list;
 
type
num corresponds to a numeric value, as in num($variable), and can only be assigned numeric values.
str corresponds to a string value, just as a global $variable. The difference is the scope.
name-list
Optionally, the name can be followed by one or more dimension specifications within square brackets, [], for example to declare an array variable.
Example 7
num i, j;
str name, company, country;
 
Initialization
Scalar variables (as opposed to e.g. array variables) can be declared and initialized simultaneously.
Note:
If you do not initialize a local variable when you declare it, a numeric variable has value 0 and a string variable is empty, until you give them values.
Example 8
num i, j, count = 3;
str name="Tom", company="OpenText";
 
Reserved keywords
You can not use the following keywords as variable names:
and, begin, bool, break, case, continue, default, do, else, end, false, for, goto, if, int, not, num, return, str, struct, switch, true, while.
Nested compound statements
Compound statements of a script can be nested. A local variable is passed to all child compound statements of the compound statement where it is declared. You can re-initialize or re-declare a local variable in a child compound statement, but the variable will retain its previous state when the child compound statement goes out of scope.
Example 9
Script 1 "Event" //This is the outermost compound statement type
where a local variable can exist, in this case
a "retrieved" script.
  str myStr = "Hello World!"; //Outermost declaration
  { //comp. statement 1
    {//comp. statement 2, nested inside if 1.
      log(0, "myStr: " + myStr); // "myStr: Hello World!"
The value is passed on from
where it is declared and
initialized.
    }
  }
  { //comp. statement 3
    str myStr = "Hello Kitty!"; //local declaration in 
comp. statement 3
    log(0, "myStr: " + myStr);  // "myStr: Hello Kitty!"
  }
  log(0, "myStr: " + myStr); // "myStr: Hello World!" again, 
because comp. statement 3 went
out of scope
 
Using begin/end
As an alternative to {} you can use Begin/End to set limits on your compound statement.
Example 10
Begin
    num i, j=1;
    while (i++ < 10)
       log(j, "still in the loop");
End
 
OpenText StreamServe 5.6 Updated: 2013-03-01