Search code examples
arraysstringinputrecordcobol

COBOL Unstring into an Array


I'm trying to unstring my input-line which is seperated with ";", into an array. But i'm having trouble with displaying every word after the first ";".

So basically, Input: Hello;Stack;Overflow


Output: Value: 2 (because of 2 semicolons)


Line 1 of my Record Table: Hello


Line 2 of my Record Table: Stack


Line 3 of my Record Table: Overflow

My code so far:

*-------------------------------------------------------------
 LINKAGE SECTION.                                             
 01  X-INPUT-LINE                   PIC X(2000).              
 01  X-SEP-CHAR                     PIC X(1).                                     
 01  X-RET-TABLE.                                             
  02 CMAX                           PIC 9(5) COMP-3.          
  02 ENTRY-REC OCCURS 0 TO 9999 TIMES DEPENDING ON CMAX       
                                      INDEXED   BY CIDX.      
   04 ENTRY-REC2.                                             
    07 LINEVALUE                    PIC X(100).               

PROCEDURE DIVISION USING X-INPUT-LINE
                         X-SEP-CHAR  
                         X-RET-TABLE.

MAIN SECTION.
MN-00.       
INITIALIZE WERT.                                

INSPECT X-INPUT-LINE TALLYING WERT FOR ALL      
        X-SEP-CHAR.                             
MOVE X-INPUT-LINE TO VAL.                       

ADD 1 TO WERT.                                  
PERFORM WERT TIMES                              
MOVE WERT TO LINEVALUE OF X-RET-TABLE (WERT)    
     UNSTRING VAL DELIMITED BY ";"              
     INTO STRVAL                                
     END-UNSTRING                               


IF CMAX OF X-RET-TABLE < 9999                   
   ADD  1  TO CMAX OF X-RET-TABLE               
   MOVE STRVAL TO ENTRY-REC(CMAX OF X-RET-TABLE)

END-IF                                          
END-PERFORM.    

With my following code I can only display the "Hello" in my example and the program displays it 3 times in 3 different rows.


Solution

  • Your original code nearly works.

    The main issue is, that you UNSTRING a given variable and always use the same starting point.

    He we can use WITH POINTER. Used on STRING the clause says "position where next character is stored", used on UNSTRING it says "position where next character is read from".

    Using the starting code (added the caller, actually use the given separator instead of the fixed ";" and a DISPLAY of the result), added here we add a variable for the starting point and use it.

         UNSTRING X-INPUT-LINE DELIMITED BY X-SEP-CHAR
         INTO STRVAL
         END-UNSTRING
    

    becomes

         UNSTRING X-INPUT-LINE DELIMITED BY X-SEP-CHAR
         INTO STRVAL
         WITH POINTER STARTING-POINT
         END-UNSTRING
    

    You'll also need to initialize some parts and can directly use the table item (as long as you ensure the counter doesn't get too big), providing you with the result (includes more samples).