reduce Statement
Syntax |
| |
---|---|---|
Semantics |
If the array exists, but has no array elements, an exception is raised: " | |
Substitutables |
| Can be any variable or object attribute having the type Array. |
| This expression must use the currently evaluated array element and its next neighbor and evaluates to a scalar having the same type as the result value. | |
| The expression after | |
Examples | Concatenate all strings to a list, separated by space
NONE
Reduce complex types and calculate a number of characters in a String
NONE
Calculate a sum through all array elements
NONE
Concatenate blobs
NONE
Usage of
NONE
|
Reducing Arrays with Elements of Base Type
For example, in an array of strings, you can reduce the list to a simple string consisting of a concatenation of all single elements.
Suppose you have an array of strings and you wish to copy all of them to a single string, separated by a space. To do this, use operation reduce
in combination with concat(element, " ", nextElement)
. Note that the second parameter of the concat() operation is the string that should be used as separator between any given array element and its neighbor. In this case, this is a space string literal.
set value = reduce myList using concat(element, " ", nextElement)
if single use element;
element
and nextElement
are keywords and allow you to use relative references (as opposed to normal, absolute indexes) to single elements of an array (see Get Array Element Operator []).
Based on the type of the array elements, you can use different operations. For example, elements of base type Integer can be computed (addition, subtraction, division, or multiplication).
set sum = reduce myNumbers using element + nextElement
if single use element;
Reducing Arrays with Elements of Complex Type
The same algorithm applies to arrays with elements of complex types as described in the previous chapter. Since each element
or nextElement
refers to an object of complex type, you can access the attributes of the object like any other instance (e.g. element.name
).
In the example below, array inArray contains objects of complex type ReduceStructure. Now, you wish to copy attribute strAttr of each array element to a single string separated by a semicolon. To do this, use the reduce operation in combination with concat(element, ";", nextElement)
.
set outString = reduce inArray using concat(element.stringAttr, ";", nextElement.stringAttr)
if single use element.stringAttr;
Single Array Elements
For arrays that contain only one single element, you can use the (mandatory) extension if single
use
in the reduce statement.
set reduceString = reduce stringArray using concat(element, " ", nextElement)
if single use concat("The array has only one element: ", element);
If an array has only one single element, the statement following if single use
will be evaluated. In the example above, element refers to the only string in the array. If the string was Hello World! , reduceString will end up with the content "The array has only one element: Hello World!".
Reduce Algorithm
The reduce algorithm works as follows. Suppose you want to reduce an array myNumbers containing the numbers 1, 2, and 3.
In the first iteration, the first occurrence of the element expression is initialized (here: take the first integer 1). Then the next element is evaluated and added (
+ nextElement
). The result of the first iteration is 3.In the second iteration, element refers to the intermediate result of the first iteration. Then,
nextElement
is added again, resulting in the value 6. This value is finally assigned to integer sum.