RPG/400 Help

XLATE (Translate)

*---------*---------------*-----------------*----------------*---------------*
|   CODE  |    FACTOR 1   |     FACTOR 2    |     RESULT     |  INDICATORS   |
|         |               |                 |      FIELD     |               |
*---------*---------------*-----------------*----------------*---------------*
| XLATE   | From:To       | String:start    | Target         |    _ ER _     |
| (P)     |               |                 | String         |               |
*---------*---------------*-----------------*----------------*---------------*

Characters in the source string (factor 2) are translated according to the From and To strings (both in factor 1) and put into a receiver field (result field). Source characters with a match in the From string are translated to corresponding characters in the To string. XLATE starts translating the source at the location specified in factor 2 and continues character by character, from left to right. If a character of the source string exists in the From string, the corresponding character in the To string is placed in the result field. Any characters in the source field before the starting position are placed unchanged in the result field.

Factor 1 must contain the From string, followed by a colon, followed by the To string. The From and To strings can contain one of the following: a field name, array element, named constant, data structure name, literal, or table name.

Factor 2 must contain either the source string or the source string followed by a colon and the start location. The source string portion of factor 2 must be character, and can contain one of the following: a field name, array element, named constant, data structure name, data structure subfield, literal, or table name. The start location portion of factor 2 must be numeric with no decimal positions and can be a named constant, array element, field name, literal, or table name. If no start location is specified, a value of 1 is used.

The result field can be a character field, character array element, data structure or a character table. The length of the result field should be as large as the source string specified in factor 2. If the result field is larger than the source string, the result will be left adjusted. If the result field is shorter than the source string, the result field will contain the leftmost part of the translated source.

If a character in the From string is duplicated, the first occurrence (leftmost) is used.

Note:
Figurative constants cannot be used in factor 1, factor 2, or result fields. No overlapping in a data structure is allowed for factor 1 and the result field, or factor 2 and the result field.

Any valid indicator can be specified in columns 7 to 17.

If factor 2 is shorter than the result field, a P specified in the operation extender position (position 53) indicates that the result field should be padded on the right with blanks after the translation.

Columns 54 and 55 must be blank. An indicator in positions 56-57 turns on if an error occurs on the operation. Columns 58-59 must be blank.

Both factor 2 and the result field must be character or both must be DBCS. See XLATE Examples.

XLATE Examples

Figure 64. XLATE Operation

*...1....+....2....+....3....+....4....+....5....+....6....+....7...
CL0N01N02N03Factor1+++OpcdeFactor2+++ResultLenDHHiLoEqComments++++++
C*
C* The following translates the blank in NUMBER to '-'.  The result
C* in RESULT will be '999-9999'.
C*
C                     MOVE '999 9999'NUMBER  8
C           ' ':'-'   XLATENUMBER    RESULT  8

Figure 65. XLATE Operation With Named Constants

*...1....+....2....+....3....+....4....+....5....+....6....+....7...
I..............Namedconstant+++++++++C.........Fldnme.............
I*
I* In the following example, all values in STRING are translated to
I* uppercase.  As a result, RESULT='RPG DEP'.
I*
I             'ABCDEFGHIJKLMNOPQRS-  C        UP
I             'TUVWXYZ'
I             'abcdefghijklmnopqrs-  C        LO
I             'tuvwxyz'
C*
CL0N01N02N03Factor1+++OpcdeFactor2+++ResultLenDHHiLoEqComments++++++
C                     MOVE 'RpG Dep' STRING  7
C           LO:UP     XLATESTRING    RESULT       90
C*
C* In the following example all values in the string are translated
C* to lowercase.  As a result, RESULT='rpg dep'.
C*
C                     MOVE 'RpG Dep' STRING  7
C           UP:LO     XLATESTRING    RESULT       90

Figure 66. Alternate Code Page Example of XLATE

I..............Namedconstant+++++++++C.........Fldnme.............
I*  US  English alphabet.
I              'aaaaae-              C         US
I              'c-
I              'eee-
I              'iiii-
I              'n'
I*  Extended alphabet.
I              'áâàaäæ-              C         XA
I              'ç-
I              'éêè-
I              'íîïì-
I              'ñ'
CL0N01N02N03Factor1+++OpcdeFactor2+++ResultLenDHHiLoEqComments++++++
C*
C* The following examples change characters only supported in
C* certain languages to characters more widely supported.
C* The field FRANC contains names of employees in the European offices
C* of an international corporation.  The program translates those
C* characters that are not printable with a US English printer.
C* Assuming the FRANC field contains the names 'François',
C* 'Hélene', and 'Niña', they will be translated to
C* 'Francois', 'Helene', and 'Nina'.
C*
C           XA:US     XLATEFRANC     INTER1 20


[ Top of Page | Previous Page | Next Page | Table of Contents ]