MainframeSupports
tip week 6/2005:

There are a lot of builtin functions in PL/1 even though I think there are more in REXX. One of the very best in PL/1 is VERIFY. VERIFY is often used to validate the content of a character string. The example: VERIFY(X, '0123456789') will return the value 0 if the the character string variable X contains digits only. If one of the characters in X is not a digit then VERIFY returns the position of the first character in X not being a digit.

Using this knowledge it is fairly easy to contruct an expression that removes leading blanks in a character string. The following expression will do the job:

SUBSTR(X, VERIFY(' ' !! X, ' ') - 1)

To ensure that VERIFY doesn't return 0, I put a blank in front of the character string variable. Nice, isn't it? I use this trick to left justify the value of FIXED BINARY variables, when I need them as part of a character string:

DCL x CHAR(15) VAR;
DCL integer FIXED BIN(31);
...
x = ' ' !! integer;
x = substr(x, verify(x, ' '));
PUT SKIP LIST('Integer = ' !! x !! ' is a bit out of line.');

By using the expression X = ' ' !! INTEGER the conversion from binary to character is done by PL/1. When you put the blank in front of the expression at the indicated point you will avoid getting the warning "Data conversion will be done by subroutine call" even though I feel pretty sure that PL/1 will make a subroutine call anyway.

You could have used the substr expression instead of X in PUT SKIP LIST, but then you would have missed the part of how to store the left justified string value. If you replace X by INTEGER in the PUT SKIP LIST then leading blanks will occur. It is also worth notifying that if X is declared with less than 15 characters then PL/1 starts stripping digits at the end.

Previous tip in english        Sidste danske tip        Tip list