Editing Numbers in CL
August 10, 2005 Cletus the Codeslinger
Think about some of the PC-based software you use. You’ve never seen the source code. You don’t know what interesting and esoteric techniques the programmer(s) used. All you know is what you see, which is how the program performs. You’re happy when it works effectively and annoyed (or worse) when it doesn’t. In other words, you’re an end user.
Now think about the end users that use your code. They have no emotional attachments to your clever use of subfiles. They couldn’t care less that you used that obfuscated API that no one else in your shop could figure out. They’re not impressed that you didn’t use any GOTO’s or indicators. They just want your program to make their lives easier.
It is my conviction that the best ways to improve people’s lives are often simple ones. Sometimes programs are hard to use, not because the techniques to make the software easy to use were difficult to implement, but because the programmer didn’t try to make the software easier to use. Call it a lack of engineering, probably caused by an urgency to begin writing code.
Consider the occasional need to edit numbers in CL programs. Suppose a CL program is to send a message to a user indicating the number of new orders that were entered into the database during a batch run. You might use some code like this:
DCL VAR(&CUSTORDERS) TYPE(*DEC) LEN(10 0) DCL VAR(&ALPHANUM) TYPE(*CHAR) LEN(10) RTVMBRD FILE(ORDERS) NBRCURRCD(&CUSTORDERS) CHGVAR VAR(&ALPHANUM) VALUE(&CUSTORDERS) SNDMSG MSG(&ALPHANUM *BCAT 'orders were added to + the database.') TOUSR(Somebody)
The user would get a message like this:
0000000420 orders were added to the database.
The message is accurate, but the leading zeros make it less readable than it could be. I have known users to misread a zero that has an embedded dot, thinking it was an eight, and vice versa. How can we give the user a cleaner message, a message less likely to be misinterpreted?
CL has no editing capabilities, but IBM has written three APIs to edit numbers. They yield the same results you get from edit codes and edit words in RPG and DDS. They have lots of parameters, but they are not hard to use and you can read about them at the links given below.
The following solution applies the 1 (one) edit code to the number of orders that were added to the database.
DCL VAR(&CUSTORDERS) TYPE(*DEC) LEN(10 0) DCL VAR(&ALPHANUM) TYPE(*CHAR) LEN(13) DCL VAR(&EDTMASK) TYPE(*CHAR) LEN(256) DCL VAR(&EDTMASKLEN) TYPE(*CHAR) LEN(4) DCL VAR(&RCVVARLEN) TYPE(*CHAR) LEN(4) DCL VAR(&ZROBAL) TYPE(*CHAR) LEN(1) DCL VAR(&EDTCODE) TYPE(*CHAR) LEN(1) VALUE('1') DCL VAR(&CURRENCY) TYPE(*CHAR) LEN(1) DCL VAR(&SRCVARPCSN) TYPE(*CHAR) LEN(4) DCL VAR(&SRCVARDEC) TYPE(*CHAR) LEN(4) DCL VAR(&ERRORDATA) TYPE(*CHAR) LEN(16) + VALUE(X'0000000000000000') RTVMBRD FILE(ORDERS) NBRCURRCD(&CUSTORDERS) CHGVAR VAR(%BIN(&SRCVARPCSN)) VALUE(10) CHGVAR VAR(%BIN(&SRCVARDEC)) VALUE(0) CALL PGM(QECCVTEC) PARM(&EDTMASK &EDTMASKLEN + &RCVVARLEN &ZROBAL &EDTCODE &CURRENCY + &SRCVARPCSN &SRCVARDEC &ERRORDATA) CALL PGM(QECEDT) PARM(&ALPHANUM &RCVVARLEN + &CUSTORDERS *PACKED &SRCVARPCSN &EDTMASK + &EDTMASKLEN &ZROBAL &ERRORDATA) SNDMSG MSG(&ALPHANUM *BCAT 'orders were added to + the database.') TOUSR(Somebody)
QECCVTEC creates an editing mask for a field of a certain size that is to be edited with a certain edit code. This task need be done only once for a variable. QECEDT applies the edit mask to the numeric variable. This task could be done repeatedly, within a loop, for instance, as necessary. The message now looks like this:
420 orders were added to the database.
There are a few blanks on the front of the message, but I do think this is an improvement.
But maybe you don’t have time to plow through a lot of API parameters. That’s understandable. Let’s consider an even simpler solution. The following section of CL source code uses a simple loop to remove leading zeros.
DCL VAR(&CUSTORDERS) TYPE(*DEC) LEN(10 0) DCL VAR(&ALPHANUM) TYPE(*CHAR) LEN(10) RTVMBRD FILE(ORDERS) NBRCURRCD(&CUSTORDERS) CHGVAR VAR(&ALPHANUM) VALUE(&CUSTORDERS) /* remove the leading zeros from &alphanum */ TESTZERO: IF COND(%SST(&ALPHANUM 1 1) *EQ '0' *AND + %SST(&ALPHANUM 2 1) *NE ' ') THEN(DO) CHGVAR VAR(&ALPHANUM) VALUE(%SST(&ALPHANUM 2 9)) GOTO CMDLBL(TESTZERO) ENDDO SNDMSG MSG(&ALPHANUM *BCAT 'orders were added to + the database.') TOUSR(Somebody)
The message looks like this:
420 orders were added to the database.
A simple loop made a big difference in a message someone will look at every day, especially if that someone tends to confuse zeros and eights.
API Information:
thank you, cl option easy and gets it done.
Your simple solution saved me a lot of time today.
Much appreciated!