Arrays and Record Sets
Arrays
An Array is an object that contains an ordered list of other objects. For example the following creates an Array of 7 items and assigns it into the $-Variable $weekDays:
$weekDays = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
There are several ways in which Arrays are created within PhixFlow:
- The simplest case is shown above i.e. the Array is created explicitly using the [value1, value2,..] syntax.
- An Array of Integers between the values n and m can be created using the expression [n..m] e.g. [51..100] creates an Array of 50 elements [51, 52, ... 100].
- Arrays can also be created by Array Handling functions e.g. split() which turns a String into an Array.
- Finally an Array can be generated by referring to an Attribute on a Pipe when that Pipe contains several rows. In this case the Array has an entry for each row in the Pipe and the value of each entry is value of the specified Attribute. For example:
Expression | Result |
---|---|
$amounts = invoice.value, $numInvoices = countElements($amounts), $maxValue = max(invoice.value), $average1 = $maxValue / $numInvoices , $average2 = average(invoice.value) |
invoice is the name of a pipe which looks up all invoices for a customer (in fact it produces a Record Set which is discussed below. invoice.value creates an Array of all the value Attributes in invoice. E.g. if the customer had 3 invoices of value 100, 150 and 125, then invoice.value would be the Array [100, 150, 125]. countElements(), max() and average() show examples the the Arrays being used. Note that $average1 and $average2 will contain the same result. |
The majority of Arrays that you are likely to use will contain values of one type e.g. all Numbers however Arrays can contain values of different types:
$oddArray = [ 1, 4.3, "Hello", toDate("20100702"), [ "Red", "White", "Blue"] ]
Note that $oddArray[5] is itself an Array! This can be useful in some very complex processing logic.
Getting / Setting the value of an Array Element
To use the nth item in the Array, use either the syntax array[n] or getElement(array,n) or array.n :
Expression | Result |
---|---|
$day = $weekDays[3] | Sets $day to "Wednesday" |
$i = 3, $day = $weekDays[$i+2] | Sets $day to "Friday" |
$day = getElement($weekDays,4) | Sets $day to "Thursday" |
$day = $weekDays.3 | Sets $day to "Wednesday" |
$day = $weekDays[9] | Sets $day to _NULL since there is no element at position 9 |
$weekDays = _NULL, $day = $weekDays.3 | Sets $day to _NULL since there is no element at position 3 (in fact there are no elements at all) |
$weekDays = [], $day = getElement($weekDays,3) | Sets $day to _NULL since there is no element at position 3 (in fact there are no elements at all) |
Similarly, to set the value of the nth item in an Array, use setElement(array,n,value):
Expression | Result |
---|---|
setElement($months,3,"March") | Sets the 3rd element of the Array $months to "March" |
However setElement($months,13,"Lunar") would cause PhixFlow to generate an error and stop processing as the expression is trying to update an item that do not exist.
Array Operations
See Array Handling for a full list functions that can be used with Arrays. However the most frequently used functions are:
Function | Description |
---|---|
getElement() | Get the value of a specific element within an Array |
addElement() | Add a new element to the end of an Array |
countElements() | Get the number of elements within an Array |
listContains() | Provides the position of a value within an Array |
split() | Turn a String of delimited values (e.g. a comma-separated list) into an Array |
Records Sets
A Record Set is an object containing one or more rows of data (usually returned from a Pipe). Entries within Record Sets can be referred to using the name of the Attribute value. In the examples below:
- account is a Pipe which contain a single record of account details.
- invoices is a Pipe which contains several invoice records for that account.
Expression | Result |
---|---|
$accountNum = account.accountNum | account.accountNum returns the value of the Attribute accountNum within the Record Set account. Because the Pipe just returns a single value, $accountNum is a single value (not an Array). |
$invoiceNumbers = invoices.number | invoices.number returns an Array of the Attribute number within the Record Set invoices. |
$firstInvoiceNumber = invoices.number[1] | This gets the first invoice number. |
$firstInvoice = invoices[1], $firstInvoiceNumber = $firstInvoice.number | This also gets the first invoice number. Note that the Record Set invoices[1] has been assigned into a $-Variable. |
$attr = "number", $invoiceNumbers = invoices.$attr | This also creates an Array of the invoice numbers. Note that the $-Variable $attr has been set to the name of the Attribute which is then used to decide which Attribute to read from invoices. |
Syntax pipe.attribute1:attribute2
Sometimes it is useful to construct a new Record Set containing a subset of the Attributes on the original Pipe. This can be done using the syntax:
pipe.attribute1:attribute2....
This form is useful when comparing selected Attributes on one Pipe with selected Attributes on another Pipe. For example:
Expression | Result |
---|---|
eq(newAccount.accountNum:customerId:custName, oldAccount.accNumber:custId:customerName) | This returns true if the Pipes newAccount and oldAccount contain matching details for the referenced Attributes. |