开发者

Standard way to remove spaces from input in cobol?

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?


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.


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.


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.


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜