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

1

2 Answers

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:

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

1

Use POINTER to keep track of where you are at, but split the UNSTRING into three UNSTRINGs. Before each unstring loop using stored POINTER to first non-space (i.e. PERFORM VARYING from current pointer by 1 UNTIL value is not space OR end of string) and unstring from there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article
Twitter Facebook Pinterest