reto
reto

Reputation: 31

How to remove trailing spaces in COBOL

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!

Upvotes: 3

Views: 31825

Answers (5)

Joe Zitzelberger
Joe Zitzelberger

Reputation: 4263

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) ":".

Upvotes: 1

Ka Lam
Ka Lam

Reputation: 381

With Enterprise COBOL V6R2 with APAR PI99703 installed. You can use the TRIM intrinsic function.

   Identification division.
   Program-id. TESTTRIM.
   Data division.
   Working-storage section.
   1 msg pic x(20) value "Hello, World!!!   ".
   Procedure division.
       Display ">>" msg "<<"
       Display ">>" function trim(msg) "<<"
       Goback.

Output would be:

>>Hello, World!!!     <<
>>Hello, World!!!<<

Upvotes: 0

Rick Smith
Rick Smith

Reputation: 4407

If you are open to a different approach, an UNSTRING statement may eliminate leading spaces, make trailing spaces irrelevant, and save the number and its length conveniently.

Note that W-ROUNDED-AMNT is changed to ensure one leading space and at least one digit.

   WORKING-STORAGE SECTION.
   01 W-IX1                          PIC 9(8) VALUE ZERO COMP-4.

   01 W-INPUT-AMNT                   PIC 9(9)V9(5).
   01 W-ROUNDED-AMNT-X.
     03 W-ROUNDED-AMNT               PIC Z(9)9.
   01 W-X                            PIC X.
   01 W-OUTPUT-AMNT                  PIC X(9).

   PROCEDURE DIVISION.
       MOVE 123.92345 TO W-INPUT-AMNT.

       COMPUTE W-ROUNDED-AMNT ROUNDED = W-INPUT-AMNT.

       UNSTRING W-ROUNDED-AMNT-X DELIMITED ALL SPACE
           INTO W-X W-OUTPUT-AMNT COUNT W-IX1

       DISPLAY "RESULT:" W-OUTPUT-AMNT(1:W-IX1) ":".
       MOVE SPACE TO W-OUTPUT-AMNT.

       STOP RUN.

Output:

RESULT:124:

Upvotes: 0

Remko
Remko

Reputation: 359

Not sure this will not work on all compilers, but I use the following construction to remove trailing spaces and/or compute string lengths. The idea is to reverse the string you're working with, then identify the number of leading spaces. Subtract that number from the length of the string. This will also preserve any embedded spaces, if present.

WORKING-STORAGE SECTION.
01 W-IX1                          PIC 9(8) VALUE ZERO COMP-4.
01 W-TRAIL                        PIC 9(8) VALUE ZERO COMP-4.
01 W-LENGTH                       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).

PROCEDURE DIVISION.
    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.

    MOVE ZERO TO W-TRAIL.

    INSPECT FUNCTION REVERSE (W-OUTPUT-AMNT)
        TALLYING W-TRAIL
        FOR LEADING SPACE.

    SUBTRACT W-TRAIL 
        FROM LENGTH OF W-OUTPUT-AMNT
        GIVING W-LENGTH.

    DISPLAY "RESULT:" W-OUTPUT-AMNT(1:W-LENGTH) ":".

    MOVE SPACE TO W-OUTPUT-AMNT.

    STOP RUN.

Upvotes: 0

Rick Smith
Rick Smith

Reputation: 4407

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:

Upvotes: 1

Related Questions