TechTip: XLATE and Character String Manipulation

RPG
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

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

 

Issak Brender

Issak Brender has been developing and supporting RPG applications since 1993. He has worked in the retail, health care, manufacturing, and pharmaceutical industries.

 

He is currently between assignments and will answer questions accordingly.
BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  •  

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: