*TRIM not removing trailing whitespaces from variable

Hello,

I have a problem which somehow I don’t seem to be able to solve myself. In this case I got a dynamic variable of type (A) which got filled by a subprogram with a password.

That password was read from a work file and may varry in length, that is why I am using dynamic variables. Now however, I want to compress a few string variables along with the password variable into another output string. My problem is, that the password string is keeping the trailing whitespaces and I have a lot of white spaces in the print and write work file results.

Im wondering why TRIM isnt removing the trailing whitespaces in my case.

Here is the code slightly altered for public usage.

DEFINE DATA LOCAL
1 password (A) dynamic INIT <‘22 22 21 23’> /* INPUT HEX VALUE
1 #PASSWORD-A (A) dynamic /* CONVERTED PASSWORD TO ALPHANUMERIC
1 #adatext1 (A82) /* SOME VARIABLE USED FOR COMPRESSION AND OUTPUT

END-DEFINE
FORMAT LS=250 PS=250
CALLNAT ‘HEXTOA’ password #PASSWORD-A /* HEX INPUT TO A CONVERSION

COMPRESS ‘somestringnumberone’ *TRIM(#PASSWORD-A) ‘someotherstring2’ INTO #adatext1

PRINT #adatext1 /* Whitespaces still contained after PASSWORD-A
WRITE WORK FILE 1 #adatext1
CLOSE WORK 1
END

Perhaps you should add LEAVING NO at the end of your COMPRESS.

1 Like

Thank you I tried that already and it does not make any difference. The whitespaces after #PASSWORD-A still appear.

The first thing to check is the output of your subprogram. Insert
WRITE '=' PASSWORD (EM=H(8)) '=' #PASSWORD-A (EM=H(8))
after the CALLNAT. I think you’re getting non-printable characters instead of trailing spaces (H’20’).

The *TRIM function is redundant. COMPRESS is already removing trailing blanks.

I don’t see the need for dynamic variables. COMPRESS will give you a variable-length result.

1 Like

The “error” might in fact lay in the subprogram itself, as the subprogram takes a dynamic variable and converts the hex values it holds using edit mask to binary and from there on returns it in readable format to the caller.

Since I do not know in advance how long the input parameter is, I was using EDIT MASK with H(16) to convert Hex to String. The whitespaces were caused by the remainder of the H(16) of the EDIT MASK.

I know solved this issue the following way.

DEFINE DATA
PARAMETER
1 #HEX-I (A) DYNAMIC /* INPUT VALUE HEX FORMAT
1 #HEX-O (A) DYNAMIC /* OUTPUT VALUE ALPHANUMERIC READABLE
LOCAL
1 #LENGTH (N4) /* COUNTER TO STORE NUMBER OF CHARACTERS
1 #BIN (B) DYNAMIC
END-DEFINE

EXAMINE FULL #HEX-I FOR ’ ’ DELETE /* REMOVE WHITESPACES BETWEEN HEX VALUES FROM INPUT
EXAMINE FULL #HEX-I FOR ’ ’ GIVING LENGTH #LENGTH /* COUNT CHARACTERS IN VARIABLE TO BE CONVERTED
#LENGTH := #LENGTH / 2 /* DIVIDE BY 2 SINCE HEX VALUES CONSIST OF 2 VALUES

MOVE EDITED #HEX-I TO #BIN(EM=H(32))
MOVE SUBSTRING(#BIN, 1, #LENGTH) TO #HEX-O /* RETURN SUBSTRING TO NOT RETURN WHITE SPACES FROM EDIT MASK

END