Global variables are used in scripts, function files, and in Event/Process tools. The lifetime of a global variable is within the execution of a job which means they are cleared when the job is completed. This type of variable is automatically accessible from all script contexts following the context where it was first initialized or referenced. Global variables are
$-prefixed and do not have to be declared or initialized before they are used. A global variable that is not explicitly assigned a value is automatically initialized to empty string (
"").
You can create global variables in scripts and you can also assign variables to fields in an Event and use these variables in scripts. For example, if the Event contains the field
countryCode, you can create a field variable
$countryCode. When data is processed,
$countryCode gets the current value of the field
countryCode. Note that field variables affect performance, so you should only create field variables when necessary. If you only need to retrieve the field value in a script, you can use field references instead. See
Field references.
Global variables are of type string. If you assign a
num value to a global variable, this value is automatically converted to
string at runtime. The following example returns the concatenated string and not the sum of the variables:
$var1 = 12; // numerical
$var2 = 3; //numerical
$var3 = $var1 + $var2; //string "123"
To return the sum of $var1 and
$var2 in the example above you must use the script function
num() in the assignment statement for
$var3:
$var1 = 12; // numerical
$var2 = 3; //numerical
$var3 = num($var1) + num($var2); //string "15"
You must also use num() for numeric arguments in script functions. For example:
$var1 = "OpenText"; //input string
$var2 = 3; /start position in input string
$var3 = SubStr($var1, num($var2)); //substring "nText"
$counter++;
$counter--;
$counter+=2;
$counter-=2;
A global variable is defined as $variableName where
variableName can contain letters (
A-
Z,
a-
z), digits (
0-
9) and underscore (
_). Note that
variableName must begin with a letter or underscore.
You must use curly brackets ({…}) to separate a global variable from adjacent text if the variable is used within a string. For example:
$name = "Tom";
$typeof = "motor";
$result = Eval ("Dear $name, you may use the ${typeof}cycle");
You can reference a substring in a global variable by using $varname(num1[,num2])where
num1 determines the position of the first character and
num2 determines how many characters from that position to return. For example:
$name="Jim Morrison";
$firstname=$name(1,3); //substring "Jim"
If num2 is omitted, the rest of the string is returned. For example:
$name="Jim Morrison";
$surname=$name(5); //substring "Morrison"
if($countryCode = "USA")
{
$overlay = "USA.lxf";
}
$amount1 = 10;
$amount2 = 20;
$total = Num($amount1) + Num($amount2);
log(1, $total);
$firstName = "Jim";
$surName = "Morrison";
$name = $firstName + " " + $surName;
log(1, $name);
$name = "Jim Morrison";
$firstName = $name(1,3);
$surName = $name(5);
log(1, $firstName);
log(1, $surName);
•
|
Performance – To use a global variable in a numerical expression, or as an argument to a function that expects a numeric argument, you must convert the string type to a numerical value, which affects performance.
|
•
|
Re-using variables – To re-use a global variable within the scope of a job, you must first assign a value to the variable, or clear it, which could introduce errors to your Project.
|
•
|
Portability of function files – When you import a function file to your Project, the variables used within the function becomes part of the global scope. This introduces a risk that they interfere with variables used in existing scripts.
|