RPG Compares and Sequencing

The AS/400’s RPG compilers offer some interesting options for controlling the way characters are compared or sorted when you run a program. If a specific sort sequence has already been applied to a file read by the program, the file opcodes used against the file (CHAIN, SETLL, SETGT, READE, and READPE) work in accordance with the file’s sort attributes (shown in Figure 2A).

Figure 2A — File sort attributes

LANGID( *JOB | language-identifier )
CCSID( *JOB | *HEX | coded-character-set-identifier )

If you want the behavior of RPG’s compare opcodes (ANDxx, CABxx, CASxx, COMP, DOU, DOUxx, DOW, DOWxx, IF, IFxx, ORxx, WHEN, and WHENxx) to match the behavior of the file opcodes, you can use RPG’s ALTSEQ/SRTSEQ keywords to accomplish this.

The ALTSEQ/SRTSEQ keywords also let you ignore case and handle national-language special characters in compare (sequence checking), array sorting (SORTA), and lookup (LOOKUP) operations. Specify ALTSEQ(*NONE) on the D-spec to exclude an array from H-spec alternate-collating sequence.

To see this tip in action, compile the source in Figure 2B and then place the program in debug mode using the STRDBG (Start Debug) command. Then use the step function (F10) to step through the program and see how the execution is affected by the settings applied.

Figure 2B — Specifying an alternate sort sequence

 *-- Compilation spec:  ---------------------------------------*
 *   CrtRpgMod  Module( TST021MR )  DbgView( *LIST )
 *   CrtPgm     Pgm( TST021I )      Module( TST011MR )
 *-- Job run attribute:  --------------------------------------*
 *   SrtSeq( *LANGIDSHR )
 *-- Control spec:  -------------------------------------------*
H AltSeq( *EXT )
H SrtSeq( *JOBRUN )
H LangId( *JOBRUN )
H Option( *SrcStmt )
 *-- Global variables:  ---------------------------------------*
D TestChar1       s             10a   Inz( 'Smith' )
D TestChar2       s             10a   Inz( 'SMITH' )
D TestArray1      s              1a   Dim( 10 )
D TestArray2      s              1a   Dim( 10 ) AltSeq( *NONE )
 *-- Check compare:  ------------------------------------------*
C                   MoveA     'CabEdAcBDe'  TestArray1
C                   MoveA     'CabEdAcBDe'  TestArray2
C                   SortA     TestArray1
C                   SortA     TestArray2
C                   If        TestChar1  =  TestChar2
C                   Eval      *InLr      =  *On
C                   EndIf
C                   Return

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s