views:

684

answers:

3

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?

A: 

Here's a solution if you work on OpenVMS:

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

   CALL "STR$TRIM" USING BY DESCRIPTOR User-Input,
                                       User-Input,
                                       BY REFERENCE WS-STRING-LENGTH.

   MOVE User-Input TO your_string.

   MOVE another-string to your_string( WS-STRING-LENGTH + 1 ).
Luc M
A: 

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

Albert Visser
With this solution, the content of user-input won't be completely displayed if it contains a space in the middle.
Luc M
+3  A: 

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.

NealB
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.
NealB