Using Variables
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.
- In expressions, attribute names can be qualified with pipe names like this:
<pipename>.<attributename>
for examplein.recordID
. - Database collectors are a special case. They generate implicit attribute names from the names of the fields returned by the database query.
- Internal variables are predefined variables that you can use to refer to specific things; see Internal Variables.
PhixFlow will automatically add internal variables in some situations. For example, when you load data via a file collector, the table automatically includes the attribute_fileName
to indicate the original file. - 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.
For information on making $-variables reusable in an application, see Using $-variables Throughout an Application below. This page summarises which variables are available in different contexts, and explains how to create and use $-variables. See also Expression Basics.
The following tables gives some guidelines for the availability of variables, based on the PhixFlow Timing Cycle for evaluating analysis models.
Location of Expression Available Variables Input multiplier Some internal variables e.g. _toDate
$-variables declared locally in the input multiplier
Database collector SQL script %USERNAME%, %PASSWORD%
$-variables
Output Multiplier internal variables e.g. _toDate
$-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 table.
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 pipeinternal 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 table.
Variable Scoping
When writing expressions that reference variables it is important to understand whether PhixFlow has "seen" the variable yet. This is the scope of the variable.
In a table, output multiplier or output filter, the attributes have a specified order. When you run analysis on a model, PhixFlow processes each attribute in order, evaluating their expressions. This means you cannot reference:
- an attribute name before PhixFlow has processed it. So in the first attribute in a table, you cannot use a value from the third attribute in the table.
- a $-variable before PhixFlow has processed the expression where it is set.
You can only reference:
- an attribute name in a subsequent attribute
- a $-variable after it has been set or evaluated.
Internal Variables
PhixFlow has a list of Internal Variables. For example, some common ways to reference attributes in a table:
Variable Syntax Example Details .
<pipe_name>.<attribute_name>
in.AppleHarvestDate
Indicates the source of the attribute is the incoming pipe. The default name for a pipe is "in". _out _
out.<attribute_name>
_out.AppleHarvestDate
PhixFlow processes attributes in their specified order.
_
out
points PhixFlow to an attribute in the same table that is present earlier in the order list, so PhixFlow has already processed it.toDate see toDate if( _out.AppleHarvestDate < _toDate('20210101'), 1, 0)
In this case one of the arguments in an if statement is an internal variable, that indicates the date up-to which PhixFlow will process a record.
If your expression is not working, check the order of the attributes. Make sure anything the expression references has already been processed or evaluated by PhixFlow. If the expression is referencing something with a higher order number, PhixFlow has not processed it yet, so cannot find it.
In applications, some commonly-used internal variables are
- _grid: to refer to a data grid in a view or layout.
- _form: to refer to a form on a layout.
- _context: to refer to a Context Parameter
The internal variables that you can use in an expression depend on context. Remember to check the properties page for the item on which you are working; see Properties, Windows, Menus and Toolbars.
Creating and Using $-variables
Defining a $-variable
- All user-defined variables must start with the $ character.
- For multi-word variable names, use no spaces and camel case.
- It can be useful to know whether a variable is used in other expressions. As there is no way to check this, we recommend using the following naming convention:
- To distinguish between $-variables that are:
- only used in the current expression or attribute, use a lowercase first letter, for example
$percent
.
These are sometimes called local. - used in other expressions or attributes, use an uppercase first letter, for example
$Percent
.
These are sometimes called global.
- only used in the current expression or attribute, use a lowercase first letter, for example
PhixFlow assumes that you are declaring a variable and setting its value the first time it occurs in the expression.
$localVar = 'Smith' $GlobalVar = 'St.John-Smith'
Using $-Variables in Expressions
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 table.
The following expression uses do() function and $-variables for a simple calculation.
Expression Result do(
$A = 5,
$B = 2,
$A*$B
)
This expression
- Sets values for 2 $-variables: $
A
and$B
- Performs simple calculation
- Returns 10
do(
$c = 10,$A*$B*$C
)A different expression
- Sets another variable value
- References the $-variables from the previous expression
- Returns 100
When to Use $-variables
$-variables are not really needed in simple calculations. They are useful:
- in forEach loops
- calculating attibute names from pipes; see Assigning Values From a Pipe.
- to store a value from a look-up pipe, when you need the value in multiple attributes
- In the first attribute, look-up the value.
- Use a $-variable to store the value that is returned by the look-up.
- When you need the value in another attribute, reference the $-variable instead of repeating the look-up.
- in complex expressions, to break up multiple consecutive functions into separate lines. In this case, use a $-variable to store the intermediate values; see Multiline Statements and $-variables.
Using $-variables Throughout an Application
$-variables can have different "scope". They can be:
- only used withn an expression or within a attribute.
- used more widely, in multiple expressions or for different attributes.
Although the variables are the same, to distinguish between these two usages we use the terms local and global respectively.
There are some situations where you want to set a $-variable to use throughout your application. For example:
- Users can access your application on a desktop or mobile platform. The form-factor affects which styles you want want to use to display the application.
Also see Sizing and Positioning Screen Content. - You application is used by people in different countries. This could affect different aspects of the application, such as showing the relevant flag on a screen.
Also see Translating an Application Interface.
In both these examples, you want to reference a $-variable that identifies the device or country. You then use the $-variable to select an appropriate formatting rule to customise the application's appearance.
To make a $-variable available throughout the application, you need to set it on something that always happens and is then always available. You can acheive this by:
- Setting a $-variable as a context parameter on the application's open action.
- Linking the context parameter it to something on the first screen that opens, such as the application home screen.
The steps in more detail are:
- In the repository, find the action that opens the application. In its properties, add a Context Parameter; see Table-Action.
- In the context parameter's Expression field, set the $-variable; see Context Parameter.
- In the repository, find the application's first screen. Right-click and select Display. On the screen:
- From the palette drag in a simple form field onto an area.
For example, use the palette Basic Components →StringField. A field is a type of layout component; see Component. - Give the field exactly the same as the context parameter.
- In the field properties tab, at the end of the Style Settings section, tick Hidden.
- From the palette drag in a simple form field onto an area.
- To display the screen propert, right-click to display the context menu. Select Show Screen Details or Screen Options → Show Details.
- In the properties tab Basic Settings section, tick Keep Data.
You have now created a $-variable that PhixFlow always knows about, but which is not visible to users. To reference this variable in any expression or script, use:
<screen>.<field>
For example, if the screen is called HomePage, and the field is called Mobile, you would reference
HomePage.Mobile
. The field name effectively redirects PhixFlow to the context parameter where the $-variable is set.If you need to find the name of something on a screen, right-click on the component and select Show Element. PhixFlow opens the properties tab for the component.
Variable Typing
When a variable is first used, PhixFlow makes a best guess about what type of data is stored in that variable; see Parameter Types. For example:
Expression PhixFlow identifies data type $thisNumber = 4
integer $thisAddress = '123 Fake St'
string
When PhixFlow has decided what type of data is stored in a variable, you usually cannot change the type. PhixFlow will reject an expression that equates different types, such as:$thisNumber = $thisAddress
The cases where you may be able to change a data type are
- integer to a floating point value: PhixFlow may be able to convert between these.
- by using type conversion function, such as toDate. This forces a value of one type into a variable of another type.
See Also