|
||||||||
|
|
![]() |
|
|
|
|
||
|
Case-Insensitive Comparisons and Sorts in RPG and Database Files by Michael Sansoterra Over the years, in the iSeries world, I've spent lots of time fiddling with RPG code to make sure that when comparing two string values, I'm comparing apples and apples. In other words, both strings are forced to have identical alphabetic case. This often involves taking the pains to make sure that one or both strings are converted to uppercase before a comparison is made. This programmatic case change is required because the iSeries, by default, uses a binary comparison. However, this default comparison mode can be changed to a case-insensitive comparison by changing the sort sequence parameter of the Create Bound RPG Program (CRTBNDRPG) command to *LangIDShr (language identifier shared-weight).
CRTBNDRPG PGM(MYLIB/MYPGM) +
SRCFILE(MYLIB/QRPGLESRC) +
SRTSEQ(*LANGIDSHR)
Specifying this parameter will cause the iSeries to make upper- and lowercase letters have equal or shared weight for whatever language you're using. A capital A, for instance, would have the same weight or value as a lowercase a. And the string iSeries would have the same weight or value as the string ISERIES. Under the default sort sequence, the following comparison will fail: C Eval UserInput='iSeries' C If UserInput='ISERIES' By setting the SrtSeq and AltSeq keywords in the H-spec, the comparison will succeed, because RPG will ignore case: H SrtSeq(*LangIDShr) AltSeq(*Ext) Not All Op Codes Are Created Equal Unfortunately, not all op codes and built-in functions heed the SrtSeq/AltSeq specification. One such problem child is SCAN. Hopefully IBM will change this in the future so that all string comparison op codes and BIFs will function consistently. Page 182 of the V5R2 ILE RPG Reference lists op codes that will comply with an overridden sort sequence: ANDxx, COMP, CABxx, CASxx, DOU, DOUxx, DOW, DOWxx, IF, IFxx, ORxx, WHEN, WHENxx, LOOKUPxx and SORTA. The sort sequence affects not only comparison operations but also sort order. When specifying sorted compile time table or array data, for instance, the data should be ordered without respect to case. Keyed PFs and LFs Another use for *LangIDShr is found in defining keys on a physical or logical file. Consider this logical file for an employee master file keyed by employee name:
A R EMPR TEXT('Employee Master')
A PFILE(EMPLOYEE)
*
A K ENAME
When compiling this logical file with the Create Logical File (CRTLF) command, specify SRTSEQ(*LangShrID) to build a case-insensitive key on employee name. The benefit of this feature will immediately manifest itself in a program that uses the file: * Get Employee Name (EName) C ExFmt EmpInq C If Not (*In03 Or *In12) * Load Subfile starting at employee name C EName SetLL EmpR C Read EmpR Regardless of how the user enters the name in the display file, and regardless of how the name is stored in the file (any combination of case--'Smith','SMITH', etc.), the file position operation will ignore case. Note that the RPG program's SRTSEQ/ALTSEQ and the logical file's ALTSEQ parameters are independent of one another. Specifying SRTSEQ/ALTSEQ on a program only affects the program's internal comparisons, not any comparisons done by the database manager, such as a file position. Likewise, specifying ALTSEQ on a keyed file only affects database comparison and sort operations, not program comparisons. Allowing RPG and the database manager to ignore case saves programming time and lets the iSeries function consistently with other platforms that default to using case-insensitive comparisons. Michael Sansoterra is a programmer/analyst for SilverLake Resources, an IT services firm based in Grand Rapids, Michigan. E-mail: msansoterra@silver-lake.com Related Article
Editors: Howard Arner, Joe Hertvik, Ted Holt, David Morris,
Managing Editor: Shannon Pastore
Publisher and Advertising Director: Jenny Thomas
Advertising Sales Representative: Kim Reed
Contact the Editors: To contact anyone on the IT Jungle Team
Go to our contacts page and send us a message. |
|
| Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved. |