3 Top Ideas to Get String Length in COBOL Unstring

Here’re three top ideas to get string length during unstring in COBOL. The function of the unstring is it splits the input string based on a delimiter. The delimiter can be SPACE, COMMA, etc. After getting unstring’s length, you can use it for validation to avoid an exception.

Here are the clauses WITH POINTER, LENGTH OF, and COUNT IN you can use to get the length of outputs.

1. WITH POINTER

WITH POINTER clause, you will get the length of the string in the ws_count. You will have the value of ( 7 – 1), which means 6. The space count is ‘1’. Note that before using the pointer, initialize it.

MOVE   1       TO ws_count
MOVE "MY NAME"   TO ws_mystr
UNSTRING ws-mystr DELIMITED BY SPACE
INTO ws-string1, ws-string2
WITH POINTER ws-count
ON OVERFLOW DISPLAY "STRING SIZE OVERFLOWED"
NOT ON OVERFLOW DISPLAY "STRING IS NOT OVERFLOWED"
END-UNSTRING.

2. LENGTH OF

The logic below forces the ws_out to accommodate the string from the input. To do it, use the LENGTH OF clause in the unstring.

UNSTRING WS-1
 INTO WS-OUT(1:LENGTH OF WS_OUT)
END-UNSTRING

Advertisements

3. COUNT IN

The clause COUNT IN you can use to get the output string’s length. It is the preferable idea. Here the WS-COUNT1 and WS-COUNT2 will have the unstring outputs length.

01 WS-3      PIC X(10) VALUE "MY NAME".
01 WS-OUT1   PIC X(4).
01 WS-OUT2   PIC X(4).
01 WS-COUNT1 COMP.
01 WS-COUNT2 COMP.

UNSTRING WS-3
  DELIMITED BY ALL SPACES,
  INTO WS-OUT1 COUNT IN WS-COUNT1,
       WS-OUT2 COUNT IN WS-COUNT2
END-UNSTRING

Related

References

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.