Spacing between Concatenated Strings
December 13, 2002 Timothy Prickett Morgan
A neat feature of RPG’s CAT op code that I have used from time to time is its ability to insert a specified number of blanks between two concatenated values. The number of blanks is specified in Factor 2, following a string and a colon (:). The new way to concatenate in RPG is by using the plus sign (+) operator, but the plus sign doesn’t support the specified number of blanks option. One reader has developed a solution, and today I pass it along to you.
Sometimes when I’m concatenating strings together using the various %TRIM functions, I want to put one or more spaces between two values in the same field, which is a simple task:
eval name = %trimr(lastname) + ‘ ‘ + firstname.
That nice little space, enclosed in single quotes (‘), is the point I illustrate. There are times when I want to use one line of code to do a concatenation where I might have one space one time and then two, three, or even more spaces the next time. Sometimes I don’t want spaces.
I could code four or five different lines that are all the same, except for the number of spaces between the single quotes, but I prefer to have one line of code where I replace the single-quoted space constants with a variable length field. I can set the length of the spacer field to be equal to the number of spaces I want between the two values each time I run it. And here’s the coolest feature: If I set the length of the field to zero, then no spaces are inserted.
In the following code, I run an EVAL statement three times, changing the length of the variable-length field SPACER, each time.
D C1 c 'ABC' D C2 C 'DEF' D Spacer s 10 varying D X s 1 0 D Test s 20 C 0 DO 2 X C EVAL %Len(Spacer) = X C EVAL Test = C1 + Spacer + C2 C DSPLY Test C ENDDO
The %LEN function sets the length of the SPACER field to zero, one or two bytes long respectively during the three tours through the loop. The results of running this program are below:
DSPLY ABCDEF *N DSPLY ABC DEF *N DSPLY ABC DEF
— John Chadwick, Project Leader
DMC Consulting, Inc.
Thanks for sharing this idea with us, John. This technique would be even more useful when inserting large numbers of blanks between two character strings.
The only other idea I have along these lines comes from Visual Basic (VB), a language with which I spend a lot of time these days. VB has a SPACE function, which returns a string of a specified number of spaces. The following VB code inserts 57 spaces between two string values.
Dim c1 As String Dim c2 As String Dim test As String c1 = "ABC" c2 = "DEF" test = c1 & Space(57) & c2
An equivalent RPG function can be written. In the following example, SPACER is a function that returns up to 4096 blanks. The number of blanks to be inserted may be passed as a literal or variable.
H dftactgrp(*no) D Spacer pr 4096 varying D NbrOfBlanks 10u 0 value D C1 c 'ABC' D C2 C 'DEF' D X s 3 0 D Test s 128 C EVAL X = 28 C EVAL Test = C1 + Spacer(X) + C2 C EVAL Test = C1 + Spacer(98) + C2 C EVAL *INLR = *on P Spacer b D Spacer pi 4096 varying D NbrOfBlanks 10u 0 value D BlankVar s 4096 varying C EVAL %Len(BlankVar) = NbrOfBlanks C RETURN BlankVar P e
But I think I like your solution better.
ELIMINATE THE COSTS OF PRE-PRINTED FORMS, LABOR AND POSTAGE WITH
iSeries based e-Forms, e-Checks, e-Mail, e-FAX, and Document Retrieval from the Web are available as individual modules or as a complete e-Document processing ‘Suite’.
Click to Download the Complete Suite or Individual Modules today