Trimming variable in CLLE

7.5k Views Asked by At

How can you trim a variable in CLLE??

4

There are 4 best solutions below

2
On

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

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)

EDIT 2012-07-19*

In some character sets ! is used instead of |. There is also the CCSID independent *CAT operation that can be used instead. See IBM's website here and here.

0
On

In releases before %TRIM(), %TRIML() and %TRIMR() were available, ILE CL could use the 'triml' C library function:

dcl   &lPath       *int            value( 0 )

callprc  'triml'  ( +
                    &Dir           +
                    ( ' ' *byval ) +
                  ) +
            rtnval( &lPath )

The proc is found in *SRVPGM QC2UTIL1.

In both OPM and ILE CL, I've often used:

dcl   &lmsgtxt1    *dec  (   5 0 ) value( 0 )

rtvmsg     msgid( cpf9897 ) msgf( QSYS/QCPFMSG ) +
             msgdta( &SQLStm )    +
             msglen( &lmsgtxt1 )

In both cases, the resulting length can then be used in a %SST() expression to effect the actual trimming.

0
On

I know this is old, but you can use %TRIM(&myVariable)