Return to the RPG Tips
String-Handling Simplified with Variable-Length Fields
The RPG Style Guide at http://www.as400network.com/article.cfm?ID=2461 recommends using built-in functions instead of arrays to handle strings. The varying-length character type (available in V4R2 and above) can also simplify string-handling code as well as make it more efficient. I recommend using varying-length fields as CONST or VALUE parameters to every string-handling subprocedure as well as for string temporaries. (I recently worked with an RPG programmer, and we reduced the number of lines in a subprocedure from about 10 icky lines to two simple and straightforward lines, mostly through the use of varying- length parameters.) Here's a little example; not only does it look better, but it's also faster (no %TRIMs). Instead of: C EVAL Name = %TRIMR(Lib) + '/' + C %TRIMR(File) + '(' + C %TRIMR(Mbr) + ')' with variable-length fields, you can use: C EVAL Name = Lib + '/' + C File + '(' + C Mbr + ')' I thought I should check my claim that this approach is faster, and it turns out it's more than twice as fast to produce "Lib/File(Mbr)", where all variables (except "Name") are 10 characters long. Adapted from a forum message by IBM Toronto's Barbara Morris
[report a broken link by clicking here]