How to remove trailing spaces in COBOL
17,645
Solution 1
Adding another INSPECT
, with some other changes, should be all that is necessary.
01 W-IX2 PIC 9(8) VALUE ZERO COMP-4.
MOVE 0 TO W-IX2
INSPECT W-OUTPUT-AMNT TALLYING
W-IX2 FOR CHARACTERS BEFORE SPACE.
DISPLAY "RESULT:" W-OUTPUT-AMNT(1:W-IX2) ":".
Output:
RESULT:124:
Solution 2
The easy way to do this is to count backwards from the end of the field to the first non-space character.
Preform varying NDX from length of W-OUTPUT-AMT by -1
until W-OUTPUT-AMT (NDX:1) <> SPACE
or NDX = 1
DISPLAY "RESULT:" W-OUTPUT-AMNT (1:NDX) ":".
Author by
Admin
Updated on June 04, 2022Comments
-
Admin almost 2 years
I have following code:
01 W-IX1 PIC 9(8) VALUE ZERO COMP-4. 01 W-INPUT-AMNT PIC 9(9)V9(5). 01 W-ROUNDED-AMNT PIC Z(9). 01 W-TEMP-AMNT PIC X(9). 01 W-OUTPUT-AMNT PIC X(9). MOVE 123.92345 TO W-INPUT-AMNT. MOVE 1 TO W-IX1. COMPUTE W-ROUNDED-AMNT ROUNDED = W-INPUT-AMNT * 1. MOVE W-ROUNDED-AMNT TO W-TEMP-AMNT. INSPECT W-TEMP-AMNT TALLYING W-IX1 FOR LEADING SPACES. MOVE W-TEMP-AMNT(W-IX1:) TO W-OUTPUT-AMNT. DISPLAY "RESULT:" W-OUTPUT-AMNT ":". MOVE SPACE TO W-OUTPUT-AMNT.
And following output:
RESULT:124 :
My intention is to receive following output:
RESULT:124:
Would appreciate any help. Thx!
-
Simon Sobisch over 5 yearsThis would also replace embedded spaces. The only option without
TRIM
is to... do exactly what the definition of the functionTRIM
is: start from first position until a not-space is found, then start from last position until not space is found, calculate size and do a ref-mod. -
Rick Smith over 5 years@SimonSobisch - I agree that
TRIM
should be the preferred method, if available; but, in this case, there are no embedded spaces when the suggested additionalINSPECT
statement is executed. COBOL and only option is rarely correct! I'm aware of at least four ways solve this problem. The OP, having usedINSPECT
, it makes sense to go with what the OP has already used. -
Simon Sobisch over 5 yearsI agree that
INSPECT
can be used, butBEFORE SPACE
would be wrong because of (your own note) to the "possible duplicate" it would have a problem with embedded spaces (as theUNSTRING
approach would have, too). A solution withINSPECT
, that works for embedded spaces, too, would also use an extension not available everywhere with theTRAILING
phrase. Despite of multipleUNSTRING
or use ofFUNCTION REVERSE
or an intermediate left-/right-aligned field I'm only aware of a manual counting approach, please provide more approaches if you think something different works. -
Rick Smith over 5 years@SimonSobisch -There are no embedded spaces, therefore, it would have a problem with embedded spaces is a hypothetical. Hypothetically, the only place where spaces (or other non-digits) could occur would be in the original input data. In that case, I might use a "de-edit"
MOVE
to recover the number then proceed with the transformation knowing there are no embedded spaces. The older post had both alpha-numeric and numeric fields, Though not stated, the embedded spaces may have been only in the alpha-numeric fields and thus not applicable to numbers as in this post. -
Admin over 5 yearsIntresting approach. Receiving an compiler error in this case: Identifier "W-ROUNDED-AMNT (NUMERIC-EDITED)" was neither an alphanumeric, DBCS, alphanumeric or national function. The statement was discarded.
-
Rick Smith over 5 years@reto - I inadvertently used an extension that may only be available with Micro Focus.