Introduction To String Handling EXAMINE Verb INSPECT Verb STRING Verb Unstring Verb
Introduction To String Handling EXAMINE Verb INSPECT Verb STRING Verb Unstring Verb
S
d
D 1
Chapter 7
R
7.1 INTRODUCTION TO STRING HANDLING
S
d
D 2
Introduction to String handling
S
d INSPECT verb
Concatenation – STRING verb
Segmentation - UNSTRING verb
D 3
Chapter 7
7.2
R
EXAMINE VERB
S
d
D 4
EXAMINE - Purpose & Syntax for Format 1
Purpose
C
To scan a string to find the number of occurrences
of a given character in it.
It can also be used to replace some or all
R
occurrences of the said character by another
character
Syntax
S
dFormat 1:
ALL
EXAMINE {identifier-1} TALLYING LEADING
D
UNTIL FIRST
literal-1. 5
Example
C
WORKING-STORAGE SECTION.
01 EXAMINE-DATA.
05 E-1 PIC A(10) VALUE ‘ABCADCADCD’.
05 E-2 PIC 9(10) VALUE 0150250350.
………..
PROCEDURE DIVISION. R
TEST-1.
EXAMINE E-1 TALLYING ALL ‘D’.
S
d
IF TALLY NOT EQUAL TO ZERO
PERFORM PROCESS-PARA
ELSE
D
PERFORM EXIT-PARA.
6
Example contd
01 EXAMINE-DATA.
05 E-1 PIC A(10) VALUE ‘ABCADCADCD’.
C
05 E-2 PIC 9(10) VALUE 0150250350.
R
EXAMINE E-2 TALLYING LEADING 0.
S
d
EXAMINE EXAMINE-DATA TALLYING UNTIL
FIRST 2.
D 7
Format 2 - Syntax
Format 2 C
ALL
EXAMINE {identifier} REPLACING LEADING
R FIRST
UNTIL FIRST
Literal-1 BY literal-2
S
d
D 8
Format 2 - Example
01 EXAMINE-DATA.
05 E-1 PIC A(10) VALUE ‘ABCADCADCD’.
C
05 E-2 PIC 9(10) VALUE 0150250350.
R
EXAMINE E-1 REPLACING ALL “A” BY “E”.
S
d
EXAMINE E-1 REPLACING LEADING “A” BY “E”.
D 9
Format 2 - Example
01 EXAMINE-DATA. C
05 E-1 PIC A(10) VALUE ‘ABCADCADCD’.
05 E-2 PIC 9(10) VALUE 0150250350.
R
EXAMINE E-1 REPLACING FIRST “D” BY “L”.
S
d
D 10
Format 3 – Syntax & Example
C
Format 3
ALL
EXAMINE {identifier} TALLYING LEADING
R UNTIL FIRST
REPLACING BY literal-2
Example
S
d
05 E-1 PIC A(10) VALUE ‘ABCADCADCD’.
7.3
R
INSPECT VERB
S
d
D 12
Points on INSPECT verb
S
d
replaced
Used only with alphanumeric data item
D 13
Format 1 – Syntax
S
d
BEFORE identifier-4
INITIAL
AFTER literal-2
D 14
Example
S
d
INSPECT ID-1 TALLYING ID-3 LEADING “F”.
D
INITIAL “G’.
15
Example contd
S
d
INSPECT ID-1 TALLYING ID-6 FOR
CHARACTERS AFTER INITIAL “I”.
D 16
Format 2 - Syntax
INSPECT identifier-1 REPLACING
C
CHARACTERS BY {id-2/lit-1}
BEFORE
AFTER
INITIAL {id-6/lit-5}
R
ALL
S
d
LEADING {id-4/lit-3} BY {id-5/lit-4}
FIRST
BEFORE INITIAL {id-6/lit-5}
D
AFTER
17
Example
S
d
D 18
Example contd
C
77 ID-1 PIC X(15) VALUE ‘AFFFBCGHIFFFJKL’.
INSPECT ID-1 REPLACING FIRST “FFF” BY “$$
$”.
R
INSPECT ID-1 REPLACING LEADING “A” BY “&”.
S
d
D 19
Format 3 – Syntax & Example
Syntax C
INSPECT identifier-1 TALLYING
<tallying part as in format 1>
REPLACING R
<replacing part as in format 2>.
Example
S
d
INSPECT ID-1
TALLYING ID-2 FOR CHARACTERS BEFORE INITIAL “I”
REPLACING CHARACTERS BY “*” AFTER INITIAL “I”.
D 20
Chapter 7
7.4
R
STRING VERB
S
d
D 21
Purpose of STRING verb
R
To transfer characters from a string to another
string starting at some particular character position
either in the receiving field or in the sending field
S
d
D 22
Syntax
R
{id-4 / lit-4} , {id-5 / lit-5},….
DELIMITED BY {id-6 / lit-6/SIZE}
S
d
[ ON OVERFLOW imperative-statement ]
D 23
Example 1
S
STRING
d DayStr DELIMITED BY SPACES
", " DELIMITED BY SIZE
MonthStr DELIMITED BY SPACES
", " DELIMITED BY SIZE
01 StringFields.
02 Field1 PIC X(18) VALUE "Where does this go".
C
02 Field2 PIC X(30)
R
VALUE "This is the destination string".
02 Field3 PIC X(15) VALUE "Here is another".
01 StrPointers.
S
02
d StrPtr PIC 99.
02 NewPtr PIC 9.
STRING Field1 DELIMITED BY SPACES INTO Field2.
DISPLAY Field2.
D 25
Example 3
01 StringFields.
02 Field1 PIC X(18) VALUE "Where does this
C
go".
02 Field2 PIC X(30)
R
VALUE "This is the destination string".
02 Field3 PIC X(15) VALUE "Here is another".
01 StrPointers.
S
d02 StrPtr PIC 99.
02 NewPtr PIC 9.
D
DISPLAY Field2.
26
Example 4
01 StringFields.
02 Field1 PIC X(18) VALUE "Where does this go".
C
02 Field2 PIC X(30)
VALUE "This is the destination string".
R
02 Field3 PIC X(15) VALUE "Here is another".
01 StrPointers.
02 StrPtr PIC 99.
S
02
d NewPtr PIC 9.
MOVE 6 TO StrPtr.
STRING Field1, Field3 DELIMITED BY SPACE
INTO Field2 WITH POINTER StrPtr
D
ON OVERFLOW DISPLAY "String Error"
NOT ON OVERFLOW DISPLAY Field2.
27
Example 5
01 StringFields.
C
02 Field1 PIC X(18) VALUE "Where does this go".
02 Field2 PIC X(30)
VALUE "This is the destination string".
R
02 Field3 PIC X(15) VALUE "Here is another".
01 StrPointers.
02 StrPtr PIC 99.
02 NewPtr PIC 9.
S
d
STRING Field1, Field2, Field3 DELIMITED BY SPACES
INTO Field4.
DISPLAY Field4
D 28
Chapter 7
R
7.5 UNSTRING VERB
S
d
D 29
Purpose of UNSTRING verb
S
d
D 30
Syntax
UNSTRING id-1 C
DELIMITED BY [ALL] {id-2/lit-1}, OR [ALL] {id-3/lit-2}
INTO id-4 R
[ , DELIMITER IN id-5 ]
[ , COUNT IN id-6 ]
[ id-7 [ , DELIMITER IN id-8 ]
S
d
[ , COUNT IN id-9 ]
[ WITH POINTER id-10 ]
[ TALLYING IN id-11 ] [; ON OVERFLOW imperative-stmt]
D 31
Example 1
R
01 YearStr PIC XX.
01 DateStr PIC X(8).
1 9 -0 5 - 8 0
S
dACCEPT DateStr.
UNSTRING DateStr
INTO DayStr, MonthStr, YearStr
ON OVERFLOW DISPLAY "Chars Left“.
D 32
Example 2
S
d
ACCEPT DateStr.
UNSTRING DateStr DELIMITED BY "stop"
INTO DayStr, MonthStr, YearStr
D ON OVERFLOW DISPLAY "Chars Left“.
33
Example 3
01 DayStr PIC XX.
01 MonthStr PIC XX.
C
01 YearStr PIC XX.
R
01 DateStr PIC X(8). 1 9 - 0 5 / 8 0
ACCEPT DateStr.
UNSTRING DateStr
S
dDELIMITED BY "/" OR "-"
INTO DayStr DELIMITER IN Hold1
MonthStr DELIMITER IN Hold2
YearStr.
D
DISPLAY DayStr SPACE MonthStr SPACE YearStr.
DISPLAY Hold1 SPACE Hold2 34
Example 4
S
d
ACCEPT DateStr.
UNSTRING DateStr
DELIMITED BY ALL "-"
D
INTO DayStr, MonthStr, YearStr
ON OVERFLOW DISPLAY "Chars Left“.
35
Chapter 7
7.6
R
REFERENCE MODIFICATION
S
d
D 36
Reference Modification – Syntax & Example
C
To refer a part of a data field by specifying the left
most character position and length.
Syntax:
R
Data name (left-most character position: length)
Example:
S
d 01 SAMPLE-DATA.
05 S1 PIC X(8) VALUE “SOFTWARE”.
05 S2 PIC X(3).
MOVE S1(4: 3) TO S2.
D 37