An unindexed array name in an EVAL (extended factor 2 in fixed-format, or a
free-format assignment) applies the operation to every element - no explicit
index loop is needed. Three forms are supported:
- Array copy (
c = a) works forDIMarrays of any element type - character, packed, zoned, integer, unsigned and float. - Element-wise arithmetic (
a = a + b) and scalar broadcast (c = 5) are supported for numeric (packed and zoned) arrays.
The examples below use packed arrays so that all three forms apply:
Dcl-S a Packed(5:2) Dim(3);
Dcl-S b Packed(5:2) Dim(3);
Dcl-S c Packed(5:2) Dim(3);
c = a; // array copy: c(i) = a(i) for every element
a = a + b; // element-wise arithmetic: a(i) = a(i) + b(i) (+ - * / )
c = 5; // scalar broadcast: c(i) = 5 for every element
When two arrays have different dimensions, they are matched element by element from the first through the shorter array's length; any excess elements of a longer target are left unchanged (matching IBM i). A scalar has no dimension, so it broadcasts across the whole target.
Dcl-S t3 Packed(5:2) Dim(3) Inz(7.77);
Dcl-S d2 Packed(5:2) Dim(2);
d2(1) = 88.88; d2(2) = 99.99;
t3 = d2; // t3 = [88.88, 99.99, 7.77] - the third element is unchanged
The fixed-format arithmetic opcodes (ADD, SUB, MULT, DIV, Z-ADD,
Z-SUB) with an unindexed array result field are also element-wise:
ADD a b computes b(i) = b(i) + a(i) for every element, a MULT b c computes
c(i) = a(i) * b(i), and a scalar factor broadcasts. The same shorter-array
element-count rule applies. (An array factor with a scalar result is not
element-wise arithmetic - rpgc reports
TRN1053.)