Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
IDENTIFICATION DIVISION.
PROGRAM-ID. TEST.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-COUNT PIC S9(4) COMP.
88 STRING-FOUND VALUE IS +2.
01 WS-SENDING-FIELD PIC X(80).
01 WS-RECEIVING-FIELD PIC X.
PROCEDURE DIVISION.
0000-MAIN.
* Set up a test string
MOVE ALL "123" TO WS-SENDING-FIELD
MOVE "DENNY" TO WS-SENDING-FIELD(50:5)
* Check for substring
INITIALIZE WS-COUNT
UNSTRING WS-SENDING-FIELD
DELIMITED BY "DENNY"
INTO WS-RECEIVING-FIELD
WS-RECEIVING-FIELD
TALLYING WS-COUNT
IF STRING-FOUND
DISPLAY "String found"
END-IF
GOBACK
.
[\code]
The advantages are:
It works with sending fields of any length without fooling around with the receiving fields.
The IF test afterwards is generally much faster than a full string compare.
WARNING! WS-COUNT is not initialized by the UNSTRING. You must take care of that yourself!
Also note that to generalize this procedure to look for a any substring and not the hard-coded literal "DENNY", you must use a reference modified data item with the proper length set (e.g. WS-SUBSTRING(1:WS-LENGTH)) to account for trailing spaces (or use a data item reference that is exactly the right length for the DELIMITED BY).
Glenn
Brainbench MVP for COBOL II
01 edit-line PIC X(80).
01 edit-line-len PIC 9(04) COMP VALUE 80.
01 search-string PIC X(10).
01 search-string-len PIC 9(04) COMP 10.
01 match-found-flag PIC 9(04) COMP VALUE 0.
88 match-found VALUE 1.
01 sub PIC 9(04) COMP.
PERFORM VARYING sub FROM 1 BY 1
UNTIL (sub > (edit-line-len - search-string-len + 1)) OR match-found
IF edit-line (sub:search-string-len)
= search-string (1:search-string-len)
MOVE 1 TO match-found-flag
END-IF
END-PERFORM
MOVE ZEROS TO WS-CNT
INSPECT WS-STRING TALLYING WS-CNT FOR LEADING
'DEMMY'
IF WS-CNT > 0
SET DEMMY-FOUND TO TRUE
ELSE
SET NO-DEMMY-FOUND TO TRUE
END-IF