STRING AND UNSTRING IN COBOL
STRING AND UNSTRING with examples: TALLYING and COUNT options
String is used to combine two or more strings/variables in to a single string.
01 name-in. 05 first-name pic x(10) value 'Mahender ' 05 Last name pic x(10) value 'Reddy ' 05 initial pic x(2) value 'G '
If you display the ‘name-in’ it shows like ‘ Mahender Reddy G ‘ i.e: it shows the full length of each variable INCLUDING THE SPACES.
So this can be avoided by using the STRING function.
STRING FIRST-NAME DELIMITED BY SPACE LAST-NAME DELIMITED BY SPACE INITIAL INTO NAME-OUT END-STRING
Now NAME-OUT would be ‘mahenderreddyg ‘.
If we want to further modify the output , it can also be done using STRING
STRING FIRST-NAME DELIMITED BY SPACE ' ' DELIMITED BY SIZE LAST-NAME DELIMITED BY SPACE ' ' DELIMITED BY SIZE INITIAL DELIMITED BY SPACE INTO NAME-OUT END-STRING.
Now NAME-OUT would be ‘Mahender Reddy G ‘.
UNSTRING verb is used to unstring/divide the source string into different sub-strings.
If we take the same above example-
NAME-OUT is ‘Mahender Reddy G ‘ and it needs to be divided into first name, last name and initial; it can be done using unstring verb.
UNSTRING NAME-OUT DELIMITED BY SPACE INTO FIRST-NAME LAST-NAME INITIAL END-UNSTRING.
Here we have used the delimiter SPACE, but we can use any other delimiter as well.
for ex: if we have a string with name list seperated by commas and we want to exatract each name into an array.
name-in-string is ‘mahender,ramu, robert,phil,Chris’, these can be separated as below
UNSTRING NAME-IN-STRING DELIMITED BY ',' INTO NAME1 NAME2 NAME3 NAME4 NAME5 END-UNSTRRING.
More UNSTRING Examples:
01 WW-UNSTRING. 05 WW-U1 PIC XX VALUE SPACES. 05 WW-U2 PIC XXX VALUE SPACES. 05 WW-U3 PIC XXXX VALUE SPACES. 05 WW-CH-1 PIC 99. 05 WW-CH-2 PIC 99. 05 WW-CH-3 PIC 99. 01 WW-U4-INPUT PIC X(12) VALUE 'AAA B CCCC '.
UNSTRING WW-U4-INPUT DELIMITED BY SPACE INTO WW-U1 COUNT IN WW-CH-1, WW-U2 COUNT IN WW-CH-2, WW-U3 COUNT IN WW-CH-3 END-UNSTRING DISPLAY 'WW-U1 --'WW-U1 DISPLAY 'WW-U2 --'WW-U2 DISPLAY 'WW-U3 --'WW-U3 DISPLAY 'WW-CH-1--'WW-CH-1 DISPLAY 'WW-CH-2--'WW-CH-2 DISPLAY 'WW-CH-3--'WW-CH-3
WW-U1 --AA WW-U2 --B WW-U3 --CCCC WW-CH-1--03 WW-CH-2--01 WW-CH-3--04
05 WW-U4-INPUT PIC X(12) VALUE ‘AA BB CC ‘.
* there are 2 spaces in between BB & CC
WW-U1 --AA WW-U2 --BB WW-U3 -- WW-CH-1--02 WW-CH-2--02 WW-CH-3--00
For the same ABOVE INPUT VALUE, if we change the Unstring value with ‘DELIMITED BY ALL SPACES Instead of ‘delimited by SPACE.
If More than one delimiter is present in between two strings, then ALL should be used.
WW-U1 --AA WW-U2 --BB WW-U3 --CC WW-CH-1--02 WW-CH-2--02 WW-CH-3--02
05 WW-U4-INPUT PIC X(12) VALUE ' ,BBB, CC, '. UNSTRING WW-U4-INPUT DELIMITED BY ',' INTO WW-U1 COUNT IN WW-CH-1, WW-U2 COUNT IN WW-CH-2, WW-U3 COUNT IN WW-CH-3 END-UNSTRING
WW-U1 -- WW-U2 --BBB WW-U3 -- CC WW-CH-1--02 WW-CH-2--03 WW-CH-3--03
Note – Count takes Spaces also into consideration.
Example 4: Multiple delimiters by Using OR in Unstring
05 WW-U4-INPUT PIC X(12) VALUE 'AA,BBB CC, '. UNSTRING WW-U4-INPUT DELIMITED BY ',' OR SPACE INTO WW-U1 COUNT IN WW-CH-1, WW-U2 COUNT IN WW-CH-2, WW-U3 COUNT IN WW-CH-3 END-UNSTRING result: WW-U1 --AA WW-U2 --BBB WW-U3 --CC WW-CH-1--02 WW-CH-2--03 WW-CH-3--02
Example 5: Tallying Option
It gives the count of number of receiving fields. If there are 4 fields in INTO clause then the count would be 4.
UNSTRING WW-U4-INPUT DELIMITED BY SPACE INTO WW-U1 COUNT IN WW-CH-1, WW-U2 COUNT IN WW-CH-2, WW-U3 COUNT IN WW-CH-3 WW-U4 COUNT IN WW-CH-4 TALLYING IN WW-TL-1
In this case WW-TL-1 contains a value of 4.
Note: TALLYING SHOULD BE CODED for the last receiving field.
Here in the below example, Delimiter is spaces and I have given only Two receiving fields
05 WW-U4-INPUT PIC X(12) VALUE 'AA BBB C'. 05 WW-U1 PIC XX VALUE SPACES. 05 WW-U2 PIC XXXXXX VALUE SPACES.
UNSTRING WW-U4-INPUT DELIMITED BY SPACE INTO WW-U1 WW-U2
What is the value in WW-U2 after execution? Is it BBB or ‘BBB C’
It is BBB only as delimited by works as below
– First it checks the input string for any given delimiter(in our case it is Space). Once it encounters any delimiter, it extracts that portion of string in to the first receiving field.
– As we have one more receiving field, it further checks for next delimiter and once it finds the next one. It stops there and extracts that portion in to the second string.
– This process gets repeated until it has no more receiving fields.
Example 7: String and Unstring with Pointer:
05 WW-NAME-DTLS PIC X(24) VALUE 'MAHENDER REDDY HYDERABAD' 05 WW-FIRST-NAME PIC X(25) VALUE SPACES. 05 WW-LAST-NAME PIC X(25) VALUE SPACES. 05 WW-CITY PIC X(25) VALUE SPACES.
In the above input example, if the requirement is to get only first name and city into output variables. It can be achieved by using POINTER. But in this case TWO unstring statements are required.
One is to get the first name in to the corresponding field and second one is to get the city.
UNSTRING WW-NAME-DTLS DELIMITED BY SPACE INTO WW-FRST-NAME END-UNSTRING
to get the CITY in to a separate field, move the starting position of string in input filed to a counter and use that in Unstring as below
MOVE 16 TO WW-PNTR2
UNSTRING WW-NAME-DTLS DELIMITED BY SPACE INTO WW-CITY WITH POINTER WW-PNTR2 END-UNSTRING
If we code as below, First name gets populated with the CITY.
MOVE 16 TO WW-PNTR2 UNSTRING WW-NAME-DTLS DELIMITED BY SPACE INTO WW-FRST-NAME WW-CITY WITH POINTER WW-PNTR2 END-UNSTRING
Note: We can give only one pointer option in one unstring.