%CONCATARR (Concatenate Array Elements with Separator)

%CONCATARR(separator : array { : *NATURAL | *STDCHARSIZE})

%CONCATARR returns the concatenation of the elements of the array operand separated by the separator operand.

If no separator is required, specify *NONE as the first operand.

If a single blank is required as the separator, specify *BLANK or *BLANKS as the first operand.

The operands must have type alphanumeric, UCS-2, or graphic.

The second operand must be an array or array expression, including %SUBARR, %LIST, or %SPLIT. It cannot have CCSID(*HEX).

The third parameter can be *NATURAL or *STDCHARSIZE to override the current CHARCOUNT mode for the statement.
  • Specify *NATURAL to indicate that %CONCATARR operates in CHARCOUNT NATURAL mode.
  • Specify *STDCHARSIZE to indicate that %CONCATARR operates in CHARCOUNT STDCHARSIZE mode..

For information on how the CHARCOUNT mode affects concatenation, see Concatenation of data with different character sizes.

The data type and CCSID of the value returned by the built-in function depends on the data type of the operands. See Determining the Common Type of Multiple Operands.

Also see %CONCAT (Concatenate with Separator).

Examples of %CONCATARR

  1. Concatenate elements from an array with several types of separator
    
       DCL-S items VARCHAR(20) DIM(5);
       DCL-S result VARCHAR(50);
    
       items(1) = 'chair';
       items(2) = 'table';
       items(3) = 'carpet';
       items(4) = 'lamp';
       items(5) = 'sofa';
    
       result = %CONCATARR(', ' : items);
       // result = "chair, table, carpet, lamp, sofa"
    
       result = %CONCATARR(*BLANK : items);
       // result = "chair table carpet lamp sofa"
    
       result = %CONCATARR(*NONE: items);
       // result = "chairtablecarpetlampsofa"
    
  2. Join the result of %SPLIT with a new separator

    The input variable, string. has words separated by several different separators. The output variable, result, has the words separated by a comma followed by a space.

    
       DCL-S string VARCHAR(50);
       DCL-S result VARCHAR(50);
    
       string = 'chair, table. carpet. lamp sofa';
    
       result = %CONCATARR(', ' : %SPLIT(string : ',. '));
       // result = "chair, table, carpet, lamp, sofa"
    
  3. Concatenate the result of an array expression

    Array years is a numeric array. The array expression %CHAR(years) is specified as the second operand of %CONCATARR.

    
       DCL-S years int(10) dim(4);
       DCL-S result VARCHAR(50);
    
       years(1) = 1949;
       years(2) = 1991;
       years(3) = 2005;
       years(4) = 2022;
    
       result = %CONCATARR(*BLANK : %CHAR(years));
       // result = "1949 1991 2005 2022"