Search code examples
cobol

Unstring in COBOL


I am trying to split a name field into three parts into first name, mid name, last name by using UNSTRING DELIMITED BY SPACES as follows

UNSTRING WA-NAME DELIMITED BY SPACES 
INTO WA-FIRST-NAME
     WA-MID-NAME
     WA-LAST-NAME

But if my name field has more than 2 spaces the remaining words are getting missed

Example : NAME : M V S PAVAN It is showing as WA-FIRST-NAME : M WA-MID-NAME : V WA-LAST-NAME : S

But the fourth word PAVAN is missing how can i get that included in my third word. i.e, i want to include all the remaining words in WA-LAST-NAME


Solution

  • To just solve the question "how can i get that included in my third word. i.e, i want to include all the remaining words in WA-LAST-NAME" (which may not be what you want) you can use different approaches but the best ones likely will use the POINTER (position in source field). It may uses an additional counter for the last item, leading to:

     UNSTRING WA-NAME DELIMITED BY ALL SPACES  *> just in case two spaces were used
     INTO WA-FIRST-NAME
          WA-MID-NAME
          WA-LAST-NAME COUNT STRPV *> *MOVE* the amount of target length
          WITH POINTER STRPS ON OVERFLOW
          ADD 2 TO STRPV           *> adding one to be after the text, another for space
          MOVE WA-NAME (STRPS:) TO WA-LAST-NAME (STRPV:)
    

    Complete test: http://tpcg.io/BYJXKL

    As donPablo already pointed out you won't get an 100% automated correct name result...