Standard way to remove spaces from input in cobol?
Asked Answered
I

5

7

I'm just learning COBOL; I'm writing a program that simply echos back user input. I have defined a variable as:

User-Input PIC X(30).

Later when I ACCEPT User-Input, then DISPLAY User-Input " plus some extra text", it has a bunch of spaces to fill the 30 characters. Is there a standard way (like Ruby's str.strip!) to remove the extra spaces?

Irrespirable answered 12/1, 2010 at 16:5 Comment(0)
B
6

One would hope for a more elegant way of simply trimming text strings but this is pretty much the standard solution... The trimming part is done in the SHOW-TEXT paragraph.


      *************************************                    
      * TRIM A STRING... THE HARD WAY...                       
      *************************************                    
       IDENTIFICATION DIVISION.                                
       PROGRAM-ID. TESTX.                                      
       DATA DIVISION.                                          
       WORKING-STORAGE SECTION.                                
       01  USER-INPUT         PIC X(30).                       
       01  I                  PIC S9(4) BINARY.                
       PROCEDURE DIVISION.                                     
           MOVE SPACES TO USER-INPUT                           
           PERFORM SHOW-TEXT                                   

           MOVE '  A B C' TO USER-INPUT                        
           PERFORM SHOW-TEXT                                   

           MOVE 'USE ALL 30 CHARACTERS -------X' TO USER-INPUT 
           PERFORM SHOW-TEXT                                 
           GOBACK                                            
           .                                                 
       SHOW-TEXT.                                            
           PERFORM VARYING I FROM LENGTH OF USER-INPUT BY -1 
                     UNTIL I LESS THAN 1 OR USER-INPUT(I:1) NOT = ' '
           END-PERFORM                                       
           IF I > ZERO                                       
              DISPLAY USER-INPUT(1:I) '@ OTHER STUFF'        
           ELSE                                              
              DISPLAY '@ OTHER STUFF'                        
           END-IF                                            
           .                                                 

Produces the following output:


@ OTHER STUFF                              
  A B C@ OTHER STUFF                       
USE ALL 30 CHARACTERS -------X@ OTHER STUFF

Note that the PERFORM VARYING statement relies on the left to right evaluation of the UNTIL clause to avoid out-of-bounds subscripting on USER-INPUT in the case where it contains only blank spaces.

Baluster answered 12/1, 2010 at 17:22 Comment(2)
Note: In the above answer I assumed that you wanted to remove trailing spaces only. If you want to remove leading spaces, a similar approach can be used starting at the front of USER-INPUT or you could try playing with the INSPECT verb with TALLYING and LEADING modifiers. Sorry, but sting management is not one of COBOL's strong points.Baluster
" this is pretty much the standard solution", except LENGTH OF is a non-standard extension.Addams
C
4

Use OpenCOBOL 1.1 or greater.

 Identification division.
 Program-id. 'trimtest'.
*> Compile:
*> cobc -x -free -ffunctions-all  TrimTest.cbl
*>
 Data division.
 Working-Storage Section.
1 myBigStr Pic X(32768) Value Spaces.

 Procedure Division.

Display "Enter Something? " With no advancing.
Accept myBigStr.
Display "[" Trim(myBigStr) "]".
Goback.

The trim function also has the options; Leading or Trailing. cobc -h formore info.

Contumely answered 6/9, 2012 at 21:48 Comment(1)
Gives me an error: 'Trim' is not defined Think, you are missing a "function" before "trim", so second to last line should be Display "[" function Trim(myBigStr) "]".Ichinomiya
E
1

Here's a solution if you work on OpenVMS:

   01 WS-STRING-LENGTH                 PIC S9(04) COMP.

   CALL "STR$TRIM" USING BY DESCRIPTOR user_output,
                                       user_input,
                                       BY REFERENCE WS-STRING-LENGTH.
Edacity answered 12/1, 2010 at 16:34 Comment(1)
+1 for System Service call, sorry to necro comment but for z/OS with DB2 there are dirty ways using embedded SQL without table access as well.Kisumu
A
1

There are three ways you can do this.

  1. Use the COBOL functions to determine the string's "length". This is a mix of a couple functions. This is my preferred method, but requires declaring extra variables.
  2. Write your own function to get the "length".
  3. Use knowledge of a "terminating" string. You have to know what key characters indicates an end-of-string, like three spaces or a low-value character.

This example code demonstrates all three.

   IDENTIFICATION DIVISION.
   PROGRAM-ID. TESTPROG.
   DATA DIVISION.
   WORKING-STORAGE SECTION.
   01 ONE-A     PIC X(20) VALUE 'RALPH WIGGAM'.
   01 ONE-A-TLY PIC 9(02) VALUE ZERO.
   01 ONE-A-LEN PIC 9(02) VALUE ZERO.
   01 ONE-B     PIC X(20) VALUE 'LIKES LEARNDING'.
   01 ONE-B-TLY PIC 9(02) VALUE ZERO.
   01 ONE-B-LEN PIC 9(02) VALUE ZERO.
   01 TWO-A     PIC X(20) VALUE 'RALPH WIGGAM'.
   01 TWO-A-LEN PIC 9(02) VALUE ZERO.
   01 TWO-B     PIC X(20) VALUE 'LIKES LEARNDING'.
   01 TWO-B-LEN PIC 9(02) VALUE ZERO.
   01 THREE-A   PIC X(20) VALUE 'RALPH WIGGAM'.
   01 THREE-B   PIC X(20) VALUE 'LIKES LEARNDING'.
   01 THREE-C   PIC X(80) VALUE SPACES.
   PROCEDURE DIVISION.

       DISPLAY ' -- METHOD ONE -- '
       INSPECT FUNCTION REVERSE(ONE-A)
        TALLYING ONE-A-TLY FOR LEADING SPACES.
       SUBTRACT ONE-A-TLY FROM LENGTH OF ONE-A GIVING ONE-A-LEN.
       INSPECT FUNCTION REVERSE(ONE-B)
        TALLYING ONE-B-TLY FOR LEADING SPACES.
       SUBTRACT ONE-B-TLY FROM LENGTH OF ONE-A GIVING ONE-B-LEN.
       DISPLAY ONE-A(1:ONE-A-LEN)
               ' ' ONE-B(1:ONE-B-LEN)
               '.'.

       DISPLAY ' -- METHOD TWO -- '
       PERFORM VARYING TWO-A-LEN FROM LENGTH OF TWO-A BY -1
        UNTIL TWO-A-LEN < 1 OR TWO-A(TWO-A-LEN:1) > SPACE
       END-PERFORM.
       PERFORM VARYING TWO-B-LEN FROM LENGTH OF TWO-B BY -1
        UNTIL TWO-B-LEN < 1 OR TWO-B(TWO-B-LEN:1) > SPACE
       END-PERFORM.
       DISPLAY TWO-A(1:TWO-A-LEN)
               ' ' TWO-B(1:TWO-B-LEN)
               '.'.

       DISPLAY ' -- METHOD THREE, NAIVE -- '
  *    DELIMITING BY JUST ANY SPACES ISN'T GOOD ENOUGH.
       STRING THREE-A DELIMITED BY SPACES
              ' ' DELIMITED BY SIZE
              THREE-B DELIMITED BY SPACES
              '.' DELIMITED BY SIZE
              INTO THREE-C.
       DISPLAY THREE-C.

       DISPLAY ' -- METHOD THREE, OK -- '
       STRING THREE-A DELIMITED BY '  '
              ' ' DELIMITED BY SIZE
              THREE-B DELIMITED BY '  '
              '.' DELIMITED BY SIZE
              INTO THREE-C.
       DISPLAY THREE-C.
   EXIT-PROG.
       STOP RUN.

and the output looks like this:

 -- METHOD ONE --            
RALPH WIGGAM LIKES LEARNDING.
 -- METHOD TWO --            
RALPH WIGGAM LIKES LEARNDING.
 -- METHOD THREE, NAIVE --   
RALPH LIKES.                 
 -- METHOD THREE, OK --      
RALPH WIGGAM LIKES LEARNDING.
Antipersonnel answered 6/4, 2012 at 21:47 Comment(1)
for method 3, you may consider first replacing any 2 spaces with one, so that 2 spaces will be a true delimiter.Seism
C
0

a more general solution:

01 length pic 99.

perform varying length from 1 by 1 
  until length > 30 or user-input[length] = space
end-perform.
if length > 30
  display user-input 'plus some extra text'
else
  display user-input[1:length] 'plus some extra text'
end-if.

untested, I don't have a compiler at hand at the moment

Cheekbone answered 12/1, 2010 at 16:58 Comment(1)
With this solution, the content of user-input won't be completely displayed if it contains a space in the middle.Edacity

© 2022 - 2024 — McMap. All rights reserved.