Thursday, September 13, 2007

Using EditC and EditW in CL:

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.
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.

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.

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.

No comments: