tags:

views:

178

answers:

2

How to trim a variable in CLLE??

Thanks Arpit

+1  A: 

No standard function TRIM is available in CLLE. However,

  • If you want to glue two variables, then look at *TCAT (or |<). This command removes the blanks in between (e.g. 'xyz ' *TCAT 'uvw' becomes 'xyzuvw'
  • If you really want to trim, then try '' *TCAT $YOURVAR *TCAT '' (can't try this one myself now. No as/400 around at home ... )
  • Or use the fact that you're working with ILE CL. You can use the command CALLPRC to call a module that can do the trick! Write that module yourself with ILE RPG or COBOL.
robertnl
If you need, then I can give you a RPG example. RPG is my preferred language on the iSeries.
robertnl
A: 

Since all variables in CL are fixed length, there is no logical requirement to trim per se.

To join two value without intervening spaces, use the |< operator, and to include a single space use |>.

To find the length in characters excluding trailing spaces, you need to do a good ol' fashioned walk backwards on the value using %SST(&VAL &POS 1) to test each character position for a space. Something like:

DCL &LEN *DEC (15 0)
DCL &VAL *CHAR 50 VALUE('Some test data')
DCL &CHR15 *CHAR 15

CHGVAR &LEN 50
LOOP: IF (&LEN > 1 & %SST(&VAL &LEN 1)==' ') (DO)
    CHGVAR &LEN VALUE(&LEN - 1)
    GOTO LOOP
    ENDDO

CHGVAR &CHR15 &LEN
SNDPGMMSG ('The length is' |> &CHR15) /* Can't concat decimal values */

To simply null-terminate a value to, for example, make a call to a C function:

DCL &VAL    *CHAR 50 VALUE('Some test text')
DCL &VALNUL *CHAR 51 /* +1 for the null */
DCL &NULL   *CHAR  1 VALUE(X'00')

CHGVAR &VALNUL VALUE(&VAL |< &NULL)
Software Monkey