Manipulate and protect data quickly and easily.
There are many ways in modern RPG/RPGLE to manipulate data: BIFs, data structures, whole programs. This TechTip demonstrates examples of character string manipulations with XLATE operation code.
This is how XLATE works according to the IBM ILE RPG Language Reference:
"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. The From, To, Source, and Target strings must be of the same type."
Note that XLATE works only in fixed-format programs, and of course data should be consistent.
Programs reformat character strings by changing the ToFmt field.
* XLATE Example for character string manipulation
D FrFmt s 11A inz('ABCDEFGHIJK')
D ToFmt s 11A
D FrSsn s 11A inz('111-25-7890')
D ToSsa s 11A
D ToSsn s 11A
*
* From 111-25-7890 to 9871-52-101
C Eval ToFmt='JIHCDFEGAKB'
C FrFmt:FrSsn xlate ToFmt ToSsa
C ToSsa Dsply
C clear FrSsn
* From 9871-52-101 to 111-25-7890
* Eval ToFmt=FrFMT
C ToFmt:ToSsa XLATE FrFmt FrSsn
C FrSsn Dsply
*
C Eval *Inlr=*on
* XLATE Example for numeric values
*
D FrSsn S 9S 0 INZ(912345670)
D FrFmt S 9A INZ('ABCDEFGHJ')
D FaSsn S 9A
D ToSsn S 11A
D ToFmt S 11A
*
* From 912345670 To 912-34-5670
C Eval ToFmt='ABC-DE-FGHJ'
C Eval FaSsn=%char(FrSsn)
C FrFmt:FaSsn xlate ToFmt ToSsn
C Clear ToFmt
C Clear ToSsn
* From 912345670 To 567-43-9120
C Eval ToFmt='FGH-ED-ABCJ'
C FrFmt:FaSsn xlate ToFmt ToSsn
C Clear ToFmt
C Clear ToSsn
* From 912345670 To 91-2X5-375Y where X and Y are unknown
C Eval ToFmt='AB-CXF-DHEY'
C FrFmt:FaSsn xlate ToFmt ToSsn
*
C Eval *Inlr=*on
I've used this technique for a long time for doing quick data conversions, recreating data files from reports with heavily used edit codes and edit words, and protecting sensitive data (ID, accounts, SSN) when creating subsets of data for testing purposes. Just make sure that related files follow the same formatting values.
The From and To format values can be stored in a data area, in cross-reference files keyed by application, or even in message files. Code by itself can be used as Copybook, procedures, subroutines, or standalone programs. You can use any special characters (e.g., #, ^, &, etc.) to set formats. By switching From and To fields, you can reformat data back to its original view, as shown in the first example.
Because XLATE operates on a character-by-character basis and continues character by character from left to right, X and Y in the last example will be "plugged in" to the result field without translation. You can reformat numeric values (integers) utilizing the same technique by converting it to character, executing XLATE for specific ToFmt values, and converting back to numeric via %dec or %int BIFs.
FYI, this code was tested and executed on V5R4.
as/400, os/400, iseries, system i, i5/os, ibm i, power systems, 6.1, 7.1, V7, V6R1
LATEST COMMENTS
MC Press Online