I often find that I want to format a string in a CL variable for use on a display panel, printout, or as message text in the Send Program Message (SNDPGMMSG) command. In order to do so, I need to splice together several little pieces using the concatenation operators: *CAT, *BCAT and *TCAT.
o *CAT joins together two strings and creates a result that includes all characters in both strings, including blanks-leading, trailing and embedded.
o *TCAT first strips out the trailing blanks of the first string, then joins that with the second string. The second string is not touched.
o *BCAT strips out the trailing blanks of the first string and then inserts one blank space between the first and second strings. You may find that it helps to remember which CAT is which by keeping in mind B for blanks and T for truncate.
The Operators in Action
Now that we've got the operators' functions straight, let's buckle down to a practical examination of their use. These operators can be used anywhere character expressions are allowed-more often than not, this means the VALUE parameter of the Change Variable (CHGVAR) command.
As an example of what each can do, let's say you have the variables &LIB, &OBJ and &RESULT, as described in 1, and that you want to change &RESULT so it contains "Object &LIB/&OBJ is in use." To illustrate how each of the three concatenation operators works, I will attempt to obtain the result, using one operator at a time. None of the three attempts gives us exactly what we want; but a combination of the operators will, as the fourth example will show. We will use 'QGPL' as the value for &LIB and 'OBJ1' for &OBJ.
As an example of what each can do, let's say you have the variables &LIB, &OBJ and &RESULT, as described in Figure 1, and that you want to change &RESULT so it contains "Object &LIB/&OBJ is in use." To illustrate how each of the three concatenation operators works, I will attempt to obtain the result, using one operator at a time. None of the three attempts gives us exactly what we want; but a combination of the operators will, as the fourth example will show. We will use 'QGPL' as the value for &LIB and 'OBJ1' for &OBJ.
Using *CAT Throughout
First, we'll try to string the variables together using the concatenation operator, *CAT. For our example, we would code the attempt as follows:
CHGVAR VAR(&RESULT) VALUE('Object' *CAT + &LIB *CAT '/' *CAT &OBJ *CAT 'is in use')
This gives you the following string in &RESULT:
'ObjectQGPL /OBJ1 is in use'
The *CAT operator merely strings literals and variables together, trailing blanks and all. It does not insert a trailing blank behind a literal or variable, nor does it delete any trailing blanks from a literal or variable.
Improving With *BCAT
Our second attempt might be more successful if it contained just one trailing blank between each pair of elements. Let's use the *BCAT operator in the VALUE parameter instead of *CAT and see how close that brings us to the target result.
CHGVAR VAR(&RESULT) VALUE('Object' + *BCAT &LIB *BCAT '/' *BCAT &OBJ + *BCAT 'is in use')
This changes &RESULT to the following string:
'Object QGPL / OBJ1 is in use'
This is closer to the target string, but it's not quite there. *BCAT concatenates literals and variables together, truncating all trailing blanks while inserting a single blank between each pair of elements. The resulting string is better than our first attempt but still doesn't give us the correct format for the library/object name information.
Trying *TCAT Only
What happens if we want to truncate all the trailing blanks in each literal and variable in the string? For the third attempt, let's use the *TCAT operator exclusively.
CHGVAR VAR(&RESULT) VALUE('Object' + *TCAT &LIB *TCAT '/' *TCAT &OBJ + *TCAT 'is in use')
This gives us the following string:
'ObjectQGPL/OBJ1is in use'
When *TCAT strings literals and variables together, it totally eliminates any trailing blanks between string elements. As you can see the result runs together the literal "Object" and the actual library name retrieved from &LIB.
Combining Operators as Required
Since none of these methods yields the result we need, the best way to create our string is by combining *CAT, *BCAT and *TCAT:
CHGVAR VAR(&RESULT) VALUE('Object' + *BCAT &LIB *TCAT '/' *CAT &OBJ + *BCAT 'is in use')
This creates the target string we want in variable &RESULT:
'Object QGPL/OBJ1 is in use'
Notice the *CAT operator immediately following the '/' literal in the CHGVAR command shown above. In this particular case, we could have used *TCAT instead to achieve the same result.
Use the Right One
Finally, one observation: none of the concatenation operators removes leading blanks from literals or variables. That is, if &OBJ were to contain " OBJ2" (with a leading blank before the letter O), the results wouldn't be the same:
CHGVAR VAR(&RESULT) VALUE('Object' + *BCAT &LIB *TCAT '/' *CAT &OBJ + *BCAT 'is in use')
In this case, &RESULT would contain:
'Object QGPL/ OBJ2 is in use'
As you can see from this example, creating formatted strings in CL is not that difficult as long as you remember when to use the three different concatenation operators.
Joe Hertvik is a system administrator for a large manufacturing company outside Chicago.
REFERENCE Further information on this topic can be found in Chapter 3 of the CL Reference manual (SC41-0030-01, CD-ROM QBKA8201).
Concatenating Strings With CL
Figure 1 Variables Used in Example
DCL VAR(&LIB) TYPE(*CHAR) LEN(10) VALUE('QGPL') DCL VAR(&OBJ) TYPE(*CHAR) LEN(10) VALUE('OBJ1') DCL VAR(&RESULT) TYPE(*CHAR) LEN(80)
LATEST COMMENTS
MC Press Online