Variable: var[ … ]
Use to get or set a named variable.
The purpose of the named variable is to make it possible to work with variable values that has a broader scope than the currently executing script method.
This could e.g. be used to count or summarize values, or accumulate other kinds of information while processing an input-source.
Named variables are stored on the Data Sources stack in a global Data Source named $VARIABLES.
The scope of a named variable is the currently executing Command.
The scope of a named variable can be made global across all Commands executed by an Action, through all levels of execution.
Syntax
object var[string variableName] {get;set}
object var[string variableName, object defaultValue] {get}
bool var.Contains( variableName ) {get} (Introduced from Perfion 2024-R1)
Method Parameters
string | Name of the variable |
object | When getting a variable value, maybe the variable does not exist yet, in this case the default-value will both be returned and a variable will be created and set with the default-value. |
Scope
By default, named variables are available within the scope of the Action Command that defines them. If you define a variable within a SELECT Command, that variable will be available in other scripts within that Command.
A named variable can be made global by adding “$” in front of the name. Making a variable global makes it available anywhere in the currently running Action, including sub Actions, started with EXECUTE.ACTION or EXECUTE.ACTION.LOOP and in any parent Action that has executed the current Action.
Usage
Named variables have a wide array of use cases.
See Using Global Variables for the Action map example of the below screenshot.
Use it to save and retrieve data across different scripts; calculate some value in a script for one column in a SELECT and use it in a script for a different column.
SELECT.PERFION & SELECT.SQL
In these Commands, you can insert a variable to make dynamic SQL and Perfion Queries. You can access a variable using the {=$MyVariable} syntax. Note that variables have to be global in order to be used in other Commands than the one it was defined in.
As Data Sources in From & To Columns
In the Action mapping, you can use global variables to define file paths, both to define where to find a file in SELECT.EXCEL, CSV, XML etch and to define where to write a file in WRITE.EXCEL, CSV XML etc commands.
Remarks
Notice that a named variable gets and sets its content as the .Net object type. The advantage is that it can store any kind of value and datatype and it is very easy to set values.
However, it makes it a little more cumbersome to retrieve the values back as the desired datatype (if you need to work on them), as getting must be wrapped into a conversion method as follows:
// getting a named variable with nummeric value
double accPrice = Convert.ToDouble( var["accumulatedPrice", 0] );
// getting a named variable with string value
string mystr = "" + var["mystr", 0];
Example
The below example shows how to use a named variable to accumulate a price and create an output column with a running balance.
// getting a the accumulated price
double accPrice = Convert.ToDouble( var["accumulatedPrice", 0] );
// add the next price to the accumulated price (if there is a price)
if( HasValue )accPrice += Convert.ToDouble( Value );
// set the accumulated price to the named variable
var["accumulatedPrice"] = accPrice;
// set the accumulated price to the Output-column (will thus show a balance)
Value = accPrice;