Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Published by Scroll Versions from space WDESIGNER and version 24.0
Syntax
Code Block
languagenone
reduce anArray using <expression with element and nextElement>
    if single use <expression with element>
Semantics

reduce allows you to reduce an array having elements of base or complex types to a scalar value by recursively applying an expression to each array element and its next element.
if single use is a mandatory extension and allows to define the action for an array containing only one single element.

If the array exists, but has no array elements, an exception is raised: "The first item of Reduce is NULL".
SubstitutablesanArrayCan be any variable or object attribute having the type Array
<expression with element and next element>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.
<expression with element>The expression after if single use uses the sole array element and evaluates to a scalar having the same type as the result value.
Examples

Concatenate all strings to a list, separated by space

Code Block
languagenone
set value = reduce myList using concat(element, " ", nextElement)
            if single use element;

Reduce complex types and calculate a number of characters in a String

Code Block
languagenone
set outLength = reduce inArray 
                using element.stringAttr.stringLength()
                      + nextElement.stringAttr.stringLength()
                if single use element.stringAttr.stringLength();

Calculate a sum through all array elements

Code Block
languagenone
set sum = reduce myNumbers using element + nextElement
          if single use element;

Concatenate blobs

Code Block
languagenone
set reducedBlob = reduce blobs using concat(element, nextElement)
                  if single use element;

Usage of if single

Code Block
languagenone
set reduceString = reduce stringArray
                   using concat("many elements: ", element, " ", nextElement)
                   if single use concat("only one element: ", element);

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.

Code Block
none
none
set value = reduce myList using concat(element, " ", nextElement)
            if single use element;
Info

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).

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).

Code Block
none
none
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).

Code Block
none
none
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.

Code Block
none
none
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 .

  1. 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 .
  2. In the second iteration, element refers to the intermediate result of the first iteration. Then, nextElement is added again, resulting in value 6 . This value is finally assigned to integer sum .
Panel
titleOn this Page:
Table of Contents
Panel
titleRelated Pages:

Otp
Floatingfalse

rp