Overview
When writing expressions and scripts in PhixFlow, you can use different types of variable.
- Attribute names are like the column headings in a spreadsheet.
- You can reference an attribute using its name in other attributes, expressions and file collectors.
- Database collectors are a special case. They generate implicit attribute names from the names of the fields returned by the database query.
- In expressions, attribute names can be qualified with pipe names like this:
<pipename>.<attributename>
for examplein.recordID
.
- Internal Variables are created and populated by PhixFlow when certain actions happen.
For example,_fileName
is populated by PhixFlow and is available in the definition of stream attribute, when the streamset has data that is coming from a file collector.
See the list on the Internal Variables page. - User-defined variables are the variables you can create to hold data required for complex calculations in attributes or other expressions. These are prefixed with a $ character, so are called $-variables.
This page explains how to create and use $-variables.
Defining a $-variable
A $-variable must start with the $ character. We recommend you use a consistent style for $-variable names.
- To distinguish between $-variables that are:
- only used in the current attribute, the inital character is lower case, for example
$percent
- also used in another attribute, the initial character is capitalised, for example
$Percent
.
- only used in the current attribute, the inital character is lower case, for example
- For multi-word names, use no spaces and camel case, for example
$ThisIsCamelCase
PhixFlow assumes that you are declaring a variable and setting its value the first time it occurs in the expression.
In expressions that have complex calculations, you may need a variable to hold the result of a calculation. You can then reference the variable later in the expression, or in other attribute expressions for the same stream. In PhixFlow you can use a $-variable for this, by prefixing the variable name with the $ symbol. The following expression uses do() function. Sets values for 2 $-variables: $ A different expression sets another variable value When to Use $-variables $-variables are not really needed in simple calculations. They are useful:$localVar = 'Smith' $GlobalVar = 'St.John-Smith'
Expression Result do(
$A = 5,
$B = 2,
$A*$B
)
A
and$ B
Performs simple calculation
Returns 10do(
$c = 10, $A*$B*$C
)
References the $-variables from the previous expression
Returns 100
=
Variable Typing
When a variable is first used, PhixFlow will make a best guess about what type of data is stored in that variable. e.g.
$thisNumber = 4 // A Number $thisAddress = '123 Fake St' // A String Value
When PhixFlow has decided what type of data is stored in a variable, that type will not change. So in the above example,
$thisNumber = $thisAddress
will cause PhixFlow to reject the expression you are building. In some cases a type conversion function (e.g. toDate) can be used to force a value of one type, into a variable of another type.Note : in some cases, PhixFlow will be able to convert from one type to another. e.g. from an integer to a floating point value.
A further discussion of type assignments and full list of types is available in the Parameter Types section on the help page for Scripting Basics and a discussion on how complex expressions can be defined for attributes is available here.
Variable Scoping
When writing expressions that reference variables it is important to understand the scope of those variables.
A Variable (or an Attribute Name) can only be referenced in an Expression if that Variable has already had a value assigned to it.
Note : Attributes are evaluated in order, so Variables defined in an Attribute (or the attribute name itself) can be referenced in a later Attribute Expression, or in a dependent Expression that is referenced after that Attribute is evaluated.
E.g. you can refer in a pipe index Expression to a $variable if you have done a "lookup($variable)" in the Stream before that pipe reference (or in an Output Multiplier, or an Output Filter), but otherwise you can’t.
The following tables gives some guidelines for the availability of Variables, based on the PhixFlow Timing Cycle for evaluating Models.
Location of Expression | Available Variables |
---|---|
Input Multiplier | Some Internal Variables (e.g. _toDate) User Defined Variables (i.e. variables declared locally in the Input Multiplier) |
Database Collector SQL script | _inputMultiplier %USERNAME%, %PASSWORD% User Defined Variables |
Output Multiplier | Internal Variables (e.g. _toDate) User Defined Variables |
Attribute Expression | Internal Variables (e.g. _toDate) All <pipeName> values (e.g. inpipe.accountVal All _out.value where value is the name of the attribute and it is before the current attribute in the Stream All user defined variables created in a prior Attribute expression |
Output Filter Expression | Internal Variables (e.g. _toDate) All _out.attribute values All user defined variables created in any Attribute expression |
Pipe Index Expression on a Lookup pipe | Internal Variables <pipename>.attribute - the name on an input pipe All _out.value where value is the name of the attribute and it is before the current attribute in the Stream |