Pavan Mullapudi
Pavan Mullapudi

Reputation: 25

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

Upvotes: 0

Views: 1823

Answers (2)

Kerry Nitz
Kerry Nitz

Reputation: 11

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.

Upvotes: 0

Simon Sobisch
Simon Sobisch

Reputation: 7287

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

Upvotes: 3

Related Questions