COBOL 400 UNIT 2 - 6 (String Handlings)
COBOL 400 UNIT 2 - 6 (String Handlings)
IN
COBOL
STRING
Used for concatenation of small strings
UNSTRING
Used for splitting one string into smaller strings
INSPECT
Used for searching for a pattern in a given string
and replacing a pattern in a given string
VERBS
STRING
STRING Statement
The STRING statement strings together the partial or complete
contents of two or more data items or literals into one single
data item.
STRING identifier-1 DELIMITED BY identifier-2 INTO identifier-3
literal-1 literal-2
SIZE
WITH POINTER identifier-4
ON OVERFLOW imperative-statement-1
NOT ON OVERFLOW imperative-statement-2
END-STRING
STRING Statement
STRING identifier-1 DELIMITED BY identifier-2 INTO identifier-3
literal-1 literal-2
SIZE
Identifier-1 or literal-1 represents the sending fields.
The DELIMITED BY phrase sets the limits of the string.
identifier-2 or literal-2 are character(s) that delimit the
data to be transferred
SIZE - Transfers the complete sending area.
identifier-3 represents the receiving field
How the STRING works
When the STRING statement is executed, data is
transferred from the sending fields to the receiving field.
The order in which sending fields are processed is the
order in which they are specified.
The STRING moves characters from the source string into
the destination string from left to right. But no space filling
occurs.
When there are a number of source strings, characters are
moved from the leftmost source string first.
After STRING statement execution is completed, only that
part of the receiving field into which data was transferred
is changed, rest of the receiving field contains the data that
was present before the execution of the STRING statement.
How the STRING works
When DELIMITED BY identifier/literal is specified, the
contents of each sending item are transferred, character-
by-character, beginning with the leftmost character and
continuing until either:
A delimiter for this sending field is reached (the
delimiter itself is not transferred), or
The rightmost character of this sending field has
been transferred.
When DELIMITED BY SIZE identifier is specified, each
entire sending field is transferred to the receiving field.
When the receiving field is filled, or when all the sending
fields have been processed, the operation is ended.
When the POINTER phrase is specified, an explicit pointer
field is available to the COBOL user to control placement of
data in the receiving field.
The user must set the explicit pointer's initial value, which
must not be less than 1 and not more than the character
count of the receiving field.
STRING identifier-1 DELIMITED BY identifier-2 INTO identifier-3
literal-1 literal-2
SIZE
WITH POINTER identifier-4
The OVERFLOW option specifies the operation(s) to
be performed if the receiving field is not large enough
to accommodate the result.
imperative-statement-1 is Executed when the pointer
value (explicit or implicit):
- Is less than 1
- Exceeds a value equal to the length of the receiving
field. (i.e. if there are still valid characters left in the
source strings but the destination string is full)
STRING identifier-1 DELIMITED BY identifier-2 INTO identifier-3
literal-1 literal-2
SIZE
WITH POINTER identifier-4
ON OVERFLOW imperative-statement-1
NOT ON OVERFLOW imperative-statement-2
END-STRING
STRING Example 1
01 DayStr PIC XX. 5
01 MonthStr PIC X(9). J U N E
01 YearStr PIC X(4). 1 9 9 4
01 DateStr PIC X(15) VALUE ALL "-".
- - - - - - - - - - - - - - -
STRING DayStr DELIMITED BY SPACES
", " DELIMITED BY SIZE
MonthStr DELIMITED BY SPACES
", " DELIMITED BY SIZE
YearStr DELIMITED BY SIZE
INTO DateStr
END-STRING.
STRING Example 1
01 DayStr PIC XX. 5
01 MonthStr PIC X(9). J U N E
01 YearStr PIC X(4). 1 9 9 4
01 DateStr PIC X(15) VALUE ALL "-".
5 - - - - - - - - - - - - - -
STRING DayStr DELIMITED BY SPACES
", " DELIMITED BY SIZE
MonthStr DELIMITED BY SPACES
", " DELIMITED BY SIZE
YearStr DELIMITED BY SIZE
INTO DateStr
END-STRING.
STRING Example 1
01 DayStr PIC XX. 5
01 MonthStr PIC X(9). J U N E
01 YearStr PIC X(4). 1 9 9 4
01 DateStr PIC X(15) VALUE ALL "-".
5 , - - - - - - - - - - - - -
STRING DayStr DELIMITED BY SPACES
", " DELIMITED BY SIZE
MonthStr DELIMITED BY SPACES
", " DELIMITED BY SIZE
YearStr DELIMITED BY SIZE
INTO DateStr
END-STRING.
STRING Example 1
01 DayStr PIC XX. 5
01 MonthStr PIC X(9). J U N E
01 YearStr PIC X(4). 1 9 9 4
01 DateStr PIC X(15) VALUE ALL "-".
5 , J U N E - - - - - - - - -
STRING DayStr DELIMITED BY SPACES
", " DELIMITED BY SIZE
MonthStr DELIMITED BY SPACES
", " DELIMITED BY SIZE
YearStr DELIMITED BY SIZE
INTO DateStr
END-STRING.
STRING Example 1
01 DayStr PIC XX. 5
01 MonthStr PIC X(9). J U N E
01 YearStr PIC X(4). 1 9 9 4
01 DateStr PIC X(15) VALUE ALL "-".
5 , J U N E , - - - - - - - -
STRING DayStr DELIMITED BY SPACES
", " DELIMITED BY SIZE
MonthStr DELIMITED BY SPACES
", " DELIMITED BY SIZE
YearStr DELIMITED BY SIZE
INTO DateStr
END-STRING.
STRING Example 1
01 DayStr PIC XX. 5
01 MonthStr PIC X(9). J U N E
01 YearStr PIC X(4). 1 9 9 4
01 DateStr PIC X(15) VALUE ALL "-".
5 , J U N E , 1 9 9 4 - - - -
STRING DayStr DELIMITED BY SPACES
"," DELIMITED BY SIZE
MonthStr DELIMITED BY SPACES
"," DELIMITED BY SIZE
YearStr DELIMITED BY SIZE
INTO DateStr
END-STRING.
STRING Example 2
01 StrPtr PIC 99.
01 DayStr PIC XX. 5
01 MonthStr PIC X(9). J U N E
01 YearStr PIC X(4). 1 9 9 4
01 DateStr PIC X(15) VALUE ALL "-".
- - - - - - - - - - - - - - -
MOVE 1 TO StrPtr
STRING DayStr DELIMITED BY SPACES
"," DELIMITED BY SIZE
INTO DateStr WITH POINTER StrPtr
END-STRING.
STRING MonthStr DELIMITED BY SPACES
"," DELIMITED BY SIZE
INTO DateStr WITH POINTER StrPtr
END-STRING.
STRING YearStr DELIMITED BY SIZE
INTO DateStr WITH POINTER StrPtr
END-STRING.
STRING Example 2
01 StrPtr PIC 99.
01 DayStr PIC XX. 5
01 MonthStr PIC X(9). J U N E
01 YearStr PIC X(4). 1 9 9 4
01 DateStr PIC X(15) VALUE ALL "-".
5 , - - - - - - - - - - - - -
MOVE 1 TO StrPtr
STRING DayStr DELIMITED BY SPACES
," DELIMITED BY SIZE
INTO DateStr WITH POINTER StrPtr
END-STRING.
STRING MonthStr DELIMITED BY SPACES
"," DELIMITED BY SIZE
INTO DateStr WITH POINTER StrPtr
END-STRING.
STRING YearStr DELIMITED BY SIZE
INTO DateStr WITH POINTER StrPtr
END-STRING.
STRING Example 2
01 StrPtr PIC 99.
01 DayStr PIC XX. 5
01 MonthStr PIC X(9). J U N E
01 YearStr PIC X(4). 1 9 9 4
01 DateStr PIC X(15) VALUE ALL "-".
5 , J U N E , - - - - - - - -
MOVE 1 TO StrPtr
STRING DayStr DELIMITED BY SPACES
"," DELIMITED BY SIZE
INTO DateStr WITH POINTER StrPtr
END-STRING.
STRING MonthStr DELIMITED BY SPACES
"," DELIMITED BY SIZE
INTO DateStr WITH POINTER StrPtr
END-STRING.
STRING YearStr DELIMITED BY SIZE
INTO DateStr WITH POINTER StrPtr
END-STRING.
STRING Example 2
01 StrPtr PIC 99.
01 DayStr PIC XX. 5
01 MonthStr PIC X(9). J U N E
01 YearStr PIC X(4). 1 9 9 4
01 DateStr PIC X(15) VALUE ALL "-".
5 , J U N E , 1 9 9 4 - - - -
MOVE 1 TO StrPtr
STRING DayStr DELIMITED BY SPACES
"," DELIMITED BY SIZE
INTO DateStr WITH POINTER StrPtr
END-STRING.
STRING MonthStr DELIMITED BY SPACES
"," DELIMITED BY SIZE
INTO DateStr WITH POINTER StrPtr
END-STRING.
STRING YearStr DELIMITED BY SIZE
INTO DateStr WITH POINTER StrPtr
END-STRING.
01 StringFields.
02 Field1 PIC X(18) VALUE "Where does this go".
02 Field2 PIC X(30)
VALUE "This is the destination string".
02 Field3 PIC X(15) VALUE "Here is another".
01 StrPointers.
02 StrPtr PIC 99.
02 NewPtr PIC 9.
STRING Field1 DELIMITED BY SPACES
INTO Field2
END-STRING.
DISPLAY Field2.
STRING Example 3
This is the destination string
01 StringFields.
02 Field1 PIC X(18) VALUE "Where does this go".
02 Field2 PIC X(30)
VALUE "This is the destination string".
02 Field3 PIC X(15) VALUE "Here is another".
01 StrPointers.
02 StrPtr PIC 99.
02 NewPtr PIC 9.
STRING Field1 DELIMITED BY SPACES
INTO Field2
END-STRING.
DISPLAY Field2.
STRING Example 3
Whereis the destination string
01 StringFields.
02 Field1 PIC X(18) VALUE "Where does this go".
02 Field2 PIC X(30)
VALUE "This is the destination string".
02 Field3 PIC X(15) VALUE "Here is another".
01 StrPointers.
02 StrPtr PIC 99.
02 NewPtr PIC 9.
STRING Field1 DELIMITED BY SIZE
INTO Field2
END-STRING.
DISPLAY Field2.
STRING Example 4
This is the destination string
01 StringFields.
02 Field1 PIC X(18) VALUE "Where does this go".
02 Field2 PIC X(30)
VALUE "This is the destination string".
02 Field3 PIC X(15) VALUE "Here is another".
01 StrPointers.
02 StrPtr PIC 99.
02 NewPtr PIC 9.
STRING Field1 DELIMITED BY SIZE
INTO Field2
END-STRING.
DISPLAY Field2.
STRING Example 4
Where does this goation string
01 StringFields.
02 Field1 PIC X(18) VALUE "Where does this go".
02 Field2 PIC X(30)
VALUE "This is the destination string".
02 Field3 PIC X(15) VALUE "Here is another".
01 StrPointers.
02 StrPtr PIC 99.
02 NewPtr PIC 9.
MOVE 6 TO StrPtr.
STRING Field1, Field3 DELIMITED BY SPACE
INTO Field2 WITH POINTER StrPtr
ON OVERFLOW DISPLAY "String Error"
NOT ON OVERFLOW DISPLAY Field2
END-STRING.
STRING Example 5
This is the destination string
01 StringFields.
02 Field1 PIC X(18) VALUE "Where does this go".
02 Field2 PIC X(30)
VALUE "This is the destination string".
02 Field3 PIC X(15) VALUE "Here is another".
01 StrPointers.
02 StrPtr PIC 99.
02 NewPtr PIC 9.
MOVE 6 TO StrPtr.
STRING Field1, Field3 DELIMITED BY SPACE
INTO Field2 WITH POINTER StrPtr
ON OVERFLOW DISPLAY "String Error"
NOT ON OVERFLOW DISPLAY Field2
END-STRING.
STRING Example 5
This WhereHerestination string
01 StringFields.
02 Field1 PIC X(18) VALUE "Where does this go".
02 Field2 PIC X(30)
VALUE "This is the destination string".
02 Field3 PIC X(15) VALUE "Here is another".
01 StrPointers.
02 StrPtr PIC 99.
02 NewPtr PIC 9.
STRING Field1, Field2, Field3
DELIMITED BY SPACES
INTO Field4
END-STRING.
DISPLAY Field4
STRING Example 6
01 StringFields.
02 Field1 PIC X(18) VALUE "Where does this go".
02 Field2 PIC X(30)
VALUE "This is the destination string".
02 Field3 PIC X(15) VALUE "Here is another".
01 StrPointers.
02 StrPtr PIC 99.
02 NewPtr PIC 9.
STRING Field1, Field2, Field3
DELIMITED BY SPACES
INTO Field4
END-STRING.
DISPLAY Field4
STRING Example 6
WhereThisHere
01 StringFields.
02 Field1 PIC X(18) VALUE "Where does this go".
02 Field2 PIC X(30)
VALUE "This is the destination string".
02 Field3 PIC X(15) VALUE "Here is another".
01 StrPointers.
02 StrPtr PIC 99.
02 NewPtr PIC 9.
MOVE 4 TO NewPtr.
STRING Field1 DELIMITED BY "this"
Field3 DELIMITED BY SPACE
"END" DELIMITED BY SIZE
INTO Field2
END-STRING.
STRING Example 7
This is the destination string
01 StringFields.
02 Field1 PIC X(18) VALUE "Where does this go".
02 Field2 PIC X(30)
VALUE "This is the destination string".
02 Field3 PIC X(15) VALUE "Here is another".
01 StrPointers.
02 StrPtr PIC 99.
02 NewPtr PIC 9.
MOVE 4 TO NewPtr.
STRING Field1 DELIMITED BY "this"
Field3 DELIMITED BY SPACE
"END" DELIMITED BY SIZE
INTO Field2
END-STRING.
STRING Example 7
Where does HereENDation string
01 StringFields.
02 Field1 PIC X(18) VALUE "Where does this go".
02 Field2 PIC X(30)
VALUE "This is the destination string".
02 Field3 PIC X(15) VALUE "Here is another".
01 StrPointers.
02 StrPtr PIC 99.
02 NewPtr PIC 9.
MOVE 4 TO NewPtr.
STRING Field1 DELIMITED BY "this"
Field3 DELIMITED BY SPACE
"Tom" DELIMITED BY SIZE
INTO Field2 WITH POINTER NewPtr
ON OVERFLOW DISPLAY "String Error"
NOT ON OVERFLOW DISPLAY Field2
END-STRING.
STRING Example 8
This is the destination string
01 StringFields.
02 Field1 PIC X(18) VALUE "Where does this go".
02 Field2 PIC X(30)
VALUE "This is the destination string".
02 Field3 PIC X(15) VALUE "Here is another".
01 StrPointers.
02 StrPtr PIC 99.
02 NewPtr PIC 9.
MOVE 4 TO NewPtr.
STRING Field1 DELIMITED BY "this"
Field3 DELIMITED BY SPACE
"Tom" DELIMITED BY SIZE
INTO Field2 WITH POINTER NewPtr
ON OVERFLOW DISPLAY "String Error"
NOT ON OVERFLOW DISPLAY Field2
END-STRING.
STRING Example 8
ThiWhere does HereTomon string
UNSTRING
UNSTRING Statement
The UNSTRING statement causes contiguous data in a
sending field to be separated and placed into multiple
receiving fields.
UNSTRING identifier-1 DELIMITED BY ALL identifier-2 OR ALL identifier-3
literal-2 literal-3
INTO identifier-4 DELIMITER IN identifier-5 COUNT IN identifier-6
WITH POINTER identifier-7 TALLYING IN identifier-8
ON OVERFLOW imperative-statement-1
NOT ON OVERFLOW imperative-statement-2
END-UNSTRING
UNSTRING identifier-1 DELIMITED BY ALL identifier-2 OR ALL identifier-3
literal-2 literal-3
Identifier-1 represents the sending field. It must be an
alphanumeric data item.
The delimiters are identifier-2, identifier-3, or their
corresponding literals.
DELIMITED BY Phrase specifies delimiters within the
data that control the data transfer.
When the ALL phrase is included, one or more
occurrences of the literal or identifier are treated as just
one occurrence.
Identifier-4 represents the data receiving fields. These fields can be
defined as alphabetic, alphanumeric and numeric.
Unless the DELIMITED BY phrase is specified, the DELIMITER IN and
COUNT IN phrases must not be specified.
identifier-5 represents the data receiving fields. A DELIMITER IN
clause is associated with a particular Destination String. Identifier-5
holds the Delimiter that was encountered in the Source String.
Identifier-6 is the data-count field for each data transfer. The COUNT
IN clause is associated with a particular Destination String and holds a
count of the number of characters passed to the Destination String.
The delimiters are not included in this count.
UNSTRING identifier-1 DELIMITED BY ALL identifier-2 OR ALL identifier-3
literal-2 literal-3
INTO identifier-4 DELIMITER IN identifier-5 COUNT IN identifier-6
END-UNSTRING
Identifier-7 contains a value that indicates a relative position in the
sending field. When this phrase is specified, the user must initialize
this field before execution of the UNSTRING statement is begun.
When the POINTER phrase is specified, the value of the pointer field
behaves as if it were increased by 1 for each examined character in
the sending field.
Identifier-8 holds a count of the number of Destination Strings affected
by the UNSTRING operation. Only one TALLYING clause can be
used with each UNSTRING. When this phrase is specified, the user
must initialize this field before execution of the UNSTRING statement
is begun.
UNSTRING identifier-1 DELIMITED BY ALL identifier-2 OR ALL identifier-3
literal-2 literal-3
INTO identifier-4 DELIMITER IN identifier-5 COUNT IN identifier-6
WITH POINTER identifier-7 TALLYING IN identifier-8
END-UNSTRING
The ON OVERFLOW is activated if :-
The pointer value (explicit or implicit) is less than 1
The pointer value (explicit or implicit) exceeds a value equal to the
length of the sending field
All data receiving fields have been acted upon, and the sending
field still contains unexamined characters
UNSTRING identifier-1 DELIMITED BY ALL identifier-2 OR ALL identifier-3
literal-2 literal-3
INTO identifier-4 DELIMITER IN identifier-5 COUNT IN identifier-6
WITH POINTER identifier-7 TALLYING IN identifier-8
ON OVERFLOW imperative-statement-1
NOT ON OVERFLOW imperative-statement-2
END-UNSTRING
How the UNSTRING works
The UNSTRING copies characters from the Source
String to the Destination String until a Delimiter is
encountered in the Source String or the Destination
String is full.
When either of these things happen the next
Destination String becomes the receiving area and
characters are copied into it until it too is full or
another Delimiter is encountered in the Source String.
Characters are copied from the Source String to the
Destination Strings according to the rules for
Alphanumeric moves. There is space filling.
UNSTRING Termination
The UNSTRING statement terminates when:-
All the characters in the Source String have been
examined
OR
All the Destination Strings have been processed
OR
Some error condition is encountered.
UNSTRING Example 1
01 DayStr PIC XX.
01 MonthStr PIC XX.
01 YearStr PIC XX.
01 DateStr PIC X(8). 1 9 - 0 5 - 8 0
ACCEPT DateStr.
UNSTRING DateStr
INTO DayStr, MonthStr, YearStr
ON OVERFLOW DISPLAY "Chars Left"
END-UNSTRING.
UNSTRING Example 1
01 DayStr PIC XX. 1 9
01 MonthStr PIC XX.
01 YearStr PIC XX.
01 DateStr PIC X(8). 1 9 - 0 5 - 8 0
ACCEPT DateStr.
UNSTRING DateStr
INTO DayStr, MonthStr, YearStr
ON OVERFLOW DISPLAY "Chars Left"
END-UNSTRING.
UNSTRING Example 1
01 DayStr PIC XX. 1 9
01 MonthStr PIC XX. - 0
01 YearStr PIC XX.
01 DateStr PIC X(8). 1 9 - 0 5 - 8 0
ACCEPT DateStr.
UNSTRING DateStr
INTO DayStr, MonthStr, YearStr
ON OVERFLOW DISPLAY "Chars Left"
END-UNSTRING.
UNSTRING Example 1
01 DayStr PIC XX. 1 9
01 MonthStr PIC XX. - 0
01 YearStr PIC XX. 5 -
01 DateStr PIC X(8). 1 9 - 0 5 - 8 0
ACCEPT DateStr.
UNSTRING DateStr
INTO DayStr, MonthStr, YearStr
ON OVERFLOW DISPLAY "Chars Left"
END-UNSTRING.
UNSTRING Example 1
01 DayStr PIC XX. 1 9
01 MonthStr PIC XX. - 0
01 YearStr PIC XX. 5 -
01 DateStr PIC X(8). 1 9 - 0 5 - 8 0
ACCEPT DateStr.
UNSTRING DateStr
INTO DayStr, MonthStr, YearStr
ON OVERFLOW DISPLAY "Chars Left"
END-UNSTRING.
Chars Left
UNSTRING Example 2
01 DayStr PIC XX.
01 MonthStr PIC XX.
01 YearStr PIC XX.
01 DateStr PIC X(8).
1 9 s t o p 0 5 s t o p 8 0
ACCEPT DateStr.
UNSTRING DateStr DELIMITED BY "stop"
INTO DayStr, MonthStr, YearStr
ON OVERFLOW DISPLAY "Chars Left"
END-UNSTRING.
UNSTRING Example 2
01 DayStr PIC XX. 1 9
01 MonthStr PIC XX.
01 YearStr PIC XX.
01 DateStr PIC X(8).
1 9 s t o p 0 5 s t o p 8 0
ACCEPT DateStr.
UNSTRING DateStr DELIMITED BY "stop"
INTO DayStr, MonthStr, YearStr
ON OVERFLOW DISPLAY "Chars Left"
END-UNSTRING.
UNSTRING Example 2
01 DayStr PIC XX. 1 9
01 MonthStr PIC XX. 0 5
01 YearStr PIC XX.
01 DateStr PIC X(8).
1 9 s t o p 0 5 s t o p 8 0
ACCEPT DateStr.
UNSTRING DateStr DELIMITED BY "stop"
INTO DayStr, MonthStr, YearStr
ON OVERFLOW DISPLAY "Chars Left"
END-UNSTRING.
UNSTRING Example 2
01 DayStr PIC XX. 1 9
01 MonthStr PIC XX. 0 5
01 YearStr PIC XX. 8 0
01 DateStr PIC X(8).
1 9 s t o p 0 5 s t o p 8 0
ACCEPT DateStr.
UNSTRING DateStr DELIMITED BY "stop"
INTO DayStr, MonthStr, YearStr
ON OVERFLOW DISPLAY "Chars Left"
END-UNSTRING.
UNSTRING Example 3
01 DayStr PIC XX. 1 9
01 MonthStr PIC XX.
01 YearStr PIC XX.
01 DateStr PIC X(8). 1 9 - 0 5 / 8 0
ACCEPT DateStr.
UNSTRING DateStr
DELIMITED BY "/" OR "-"
INTO DayStr DELIMITER IN Hold1
MonthStr DELIMITER IN Hold2
YearStr
END-UNSTRING.
DISPLAY DayStr SPACE MonthStr SPACE YearStr.
DISPLAY Hold1 SPACE Hold2
UNSTRING Example 3
01 DayStr PIC XX. 1 9
01 MonthStr PIC XX. 0 5
01 YearStr PIC XX.
01 DateStr PIC X(8). 1 9 - 0 5 / 8 0
ACCEPT DateStr.
UNSTRING DateStr
DELIMITED BY "/" OR "-"
INTO DayStr DELIMITER IN Hold1
MonthStr DELIMITER IN Hold2
YearStr
END-UNSTRING.
DISPLAY DayStr SPACE MonthStr SPACE YearStr.
DISPLAY Hold1 SPACE Hold2
UNSTRING Example 3
01 DayStr PIC XX. 1 9
01 MonthStr PIC XX. 0 5
01 YearStr PIC XX. 8 0
01 DateStr PIC X(8). 1 9 - 0 5 / 8 0
ACCEPT DateStr.
UNSTRING DateStr
DELIMITED BY "/" OR "-"
INTO DayStr DELIMITER IN Hold1
MonthStr DELIMITER IN Hold2
YearStr
END-UNSTRING.
DISPLAY DayStr SPACE MonthStr SPACE YearStr.
DISPLAY Hold1 SPACE Hold2
UNSTRING Example 3
01 DayStr PIC XX. 1 9
01 MonthStr PIC XX. 0 5
01 YearStr PIC XX. 8 0
01 DateStr PIC X(8). 1 9 - 0 5 / 8 0
ACCEPT DateStr.
UNSTRING DateStr
DELIMITED BY "/" OR "-"
INTO DayStr DELIMITER IN Hold1
MonthStr DELIMITER IN Hold2
YearStr
END-UNSTRING.
DISPLAY DayStr SPACE MonthStr SPACE YearStr.
DISPLAY Hold1 SPACE Hold2
19 05 80
- /
UNSTRING Example 4
01 DayStr PIC XX.
01 MonthStr PIC XX.
01 YearStr PIC XX.
01 DateStr PIC X(8). 1 9 - 0 5 / 8 0
ACCEPT DateStr.
UNSTRING DateStr
DELIMITED BY "/" OR "-"
INTO DayStr DELIMITER IN Hold1
MonthStr DELIMITER IN Hold1
YearStr
END-UNSTRING.
DISPLAY DayStr SPACE MonthStr SPACE YearStr.
DISPLAY Hold1
UNSTRING Example 4
01 DayStr PIC XX. 1 9
01 MonthStr PIC XX. 0 5
01 YearStr PIC XX. 8 0
01 DateStr PIC X(8). 1 9 - 0 5 / 8 0
ACCEPT DateStr.
UNSTRING DateStr
DELIMITED BY "/" OR "-"
INTO DayStr DELIMITER IN Hold1
MonthStr DELIMITER IN Hold1
YearStr
END-UNSTRING.
DISPLAY DayStr SPACE MonthStr SPACE YearStr.
DISPLAY Hold1
19 05 80
/
UNSTRING Example 5
01 DayStr PIC XX.
01 MonthStr PIC XX.
01 YearStr PIC XX.
01 DateStr PIC X(11). 1 5 - - - 0 7 - - 9 4
ACCEPT DateStr.
UNSTRING DateStr
DELIMITED BY ALL "-"
INTO DayStr, MonthStr, YearStr
ON OVERFLOW DISPLAY "Chars Left"
END-UNSTRING.
UNSTRING Example 5
01 DayStr PIC XX. 1 5
01 MonthStr PIC XX. 0 7
01 YearStr PIC XX. 9 4
01 DateStr PIC X(11). 1 5 - - - 0 7 - - 9 4
ACCEPT DateStr.
UNSTRING DateStr
DELIMITED BY ALL "-"
INTO DayStr, MonthStr, YearStr
ON OVERFLOW DISPLAY "Chars Left"
END-UNSTRING.
UNSTRING Example 6
01 DayStr PIC XX.
01 MonthStr PIC XX.
01 YearStr PIC XX.
01 DateStr PIC X(11). 1 5 - - - 0 7 - - 9 4
ACCEPT DateStr.
UNSTRING DateStr
DELIMITED BY "-"
INTO DayStr, MonthStr, YearStr
ON OVERFLOW DISPLAY "Chars Left"
END-UNSTRING.
UNSTRING Example 6
01 DayStr PIC XX. 1 5
01 MonthStr PIC XX.
01 YearStr PIC XX.
01 DateStr PIC X(11). 1 5 - - - 0 7 - - 9 4
ACCEPT DateStr.
UNSTRING DateStr
DELIMITED BY "-"
INTO DayStr, MonthStr, YearStr
ON OVERFLOW DISPLAY "Chars Left"
END-UNSTRING.
Chars Left
UNSTRING Example 7
01 OldName PIC X(80).
01 TempName.
02 NameInitial PIC X.
02 FILLER PIC X(15).
01 NewName PIC X(30).
01 Pointers.
02 StrPtr PIC 99 VALUE 1.
02 UnstrPtr PIC 99 VALUE 1.
88 NameProcessed VALUE 81.
PROCEDURE DIVISION.
ProcessName.
ACCEPT OldName.
PERFORM UNTIL NameProcessed
UNSTRING OldName DELIMITED BY ALL SPACES
INTO TempName WITH POINTER UnstrPtr
END-UNSTRING
Display TempName
END-PERFORM
STOP RUN.
Tim John Roberts
UNSTRING Example 7
01 OldName PIC X(80).
01 TempName.
02 NameInitial PIC X.
02 FILLER PIC X(15).
01 NewName PIC X(30).
01 Pointers.
02 StrPtr PIC 99 VALUE 1.
02 UnstrPtr PIC 99 VALUE 1.
88 NameProcessed VALUE 81.
PROCEDURE DIVISION.
ProcessName.
ACCEPT OldName.
PERFORM UNTIL NameProcessed
UNSTRING OldName DELIMITED BY ALL SPACES
INTO TempName WITH POINTER UnstrPtr
END-UNSTRING
Display TempName
END-PERFORM
STOP RUN.
Tim
Tim John Roberts
T im
UNSTRING Example 7
01 OldName PIC X(80).
01 TempName.
02 NameInitial PIC X.
02 FILLER PIC X(15).
01 NewName PIC X(30).
01 Pointers.
02 StrPtr PIC 99 VALUE 1.
02 UnstrPtr PIC 99 VALUE 1.
88 NameProcessed VALUE 81.
PROCEDURE DIVISION.
ProcessName.
ACCEPT OldName.
PERFORM UNTIL NameProcessed
UNSTRING OldName DELIMITED BY ALL SPACES
INTO TempName WITH POINTER UnstrPtr
END-UNSTRING
Display TempName
END-PERFORM
STOP RUN.
John
Tim John Roberts
J ohn
UNSTRING Example 7
01 OldName PIC X(80).
01 TempName.
02 NameInitial PIC X.
02 FILLER PIC X(15).
01 NewName PIC X(30).
01 Pointers.
02 StrPtr PIC 99 VALUE 1.
02 UnstrPtr PIC 99 VALUE 1.
88 NameProcessed VALUE 81.
PROCEDURE DIVISION.
ProcessName.
ACCEPT OldName.
PERFORM UNTIL NameProcessed
UNSTRING OldName DELIMITED BY ALL SPACES
INTO TempName WITH POINTER UnstrPtr
END-UNSTRING
Display TempName
END-PERFORM
STOP RUN.
Roberts
Tim John Roberts
R oberts
UNSTRING Example 8
OldName
TempName
NewName
PROCEDURE DIVISION.
ProcessName.
ACCEPT OldName.
UNSTRING OldName DELIMITED BY ALL SPACES
INTO TempName WITH POINTER UnstrPtr
END-UNSTRING
PERFORM UNTIL NameProcessed
STRING NameInitial "." DELIMITED BY SIZE
INTO NewName WITH POINTER StrPtr
END-STRING
UNSTRING OldName DELIMITED BY ALL SPACES
INTO TempName WITH POINTER UnstrPtr
END-UNSTRING
END-PERFORM
STRING SPACE TempName DELIMITED BY SIZE
INTO NewName WITH POINTER StrPtr
END-STRING
STOP RUN.
Tim John Roberts
UNSTRING Example 8
OldName
TempName
NewName
PROCEDURE DIVISION.
ProcessName.
ACCEPT OldName.
UNSTRING OldName DELIMITED BY ALL SPACES
INTO TempName WITH POINTER UnstrPtr
END-UNSTRING
PERFORM UNTIL NameProcessed
STRING NameInitial "." DELIMITED BY SIZE
INTO NewName WITH POINTER StrPtr
END-STRING
UNSTRING OldName DELIMITED BY ALL SPACES
INTO TempName WITH POINTER UnstrPtr
END-UNSTRING
END-PERFORM
STRING SPACE TempName DELIMITED BY SIZE
INTO NewName WITH POINTER StrPtr
END-STRING
STOP RUN.
Tim John Roberts
T im
UNSTRING Example 8
OldName
TempName
NewName
PROCEDURE DIVISION.
ProcessName.
ACCEPT OldName.
UNSTRING OldName DELIMITED BY ALL SPACES
INTO TempName WITH POINTER UnstrPtr
END-UNSTRING
PERFORM UNTIL NameProcessed
STRING NameInitial "." DELIMITED BY SIZE
INTO NewName WITH POINTER StrPtr
END-STRING
UNSTRING OldName DELIMITED BY ALL SPACES
INTO TempName WITH POINTER UnstrPtr
END-UNSTRING
END-PERFORM
STRING SPACE TempName DELIMITED BY SIZE
INTO NewName WITH POINTER StrPtr
END-STRING
STOP RUN.
Tim John Roberts
T im
T .
UNSTRING Example 8
OldName
TempName
NewName
PROCEDURE DIVISION.
ProcessName.
ACCEPT OldName.
UNSTRING OldName DELIMITED BY ALL SPACES
INTO TempName WITH POINTER UnstrPtr
END-UNSTRING
PERFORM UNTIL NameProcessed
STRING NameInitial "." DELIMITED BY SIZE
INTO NewName WITH POINTER StrPtr
END-STRING
UNSTRING OldName DELIMITED BY ALL SPACES
INTO TempName WITH POINTER UnstrPtr
END-UNSTRING
END-PERFORM
STRING SPACE TempName DELIMITED BY SIZE
INTO NewName WITH POINTER StrPtr
END-STRING
STOP RUN.
Tim John Roberts
J ohn
T .
UNSTRING Example 8
OldName
TempName
NewName
PROCEDURE DIVISION.
ProcessName.
ACCEPT OldName.
UNSTRING OldName DELIMITED BY ALL SPACES
INTO TempName WITH POINTER UnstrPtr
END-UNSTRING
PERFORM UNTIL NameProcessed
STRING NameInitial "." DELIMITED BY SIZE
INTO NewName WITH POINTER StrPtr
END-STRING
UNSTRING OldName DELIMITED BY ALL SPACES
INTO TempName WITH POINTER UnstrPtr
END-UNSTRING
END-PERFORM
STRING SPACE TempName DELIMITED BY SIZE
INTO NewName WITH POINTER StrPtr
END-STRING
STOP RUN.
Tim John Roberts
J ohn
T . J .
UNSTRING Example 8
OldName
TempName
NewName
PROCEDURE DIVISION.
ProcessName.
ACCEPT OldName.
UNSTRING OldName DELIMITED BY ALL SPACES
INTO TempName WITH POINTER UnstrPtr
END-UNSTRING
PERFORM UNTIL NameProcessed
STRING NameInitial "." DELIMITED BY SIZE
INTO NewName WITH POINTER StrPtr
END-STRING
UNSTRING OldName DELIMITED BY ALL SPACES
INTO TempName WITH POINTER UnstrPtr
END-UNSTRING
END-PERFORM
STRING SPACE TempName DELIMITED BY SIZE
INTO NewName WITH POINTER StrPtr
END-STRING
STOP RUN.
Tim John Roberts
R oberts
T . J .
UNSTRING Example 8
OldName
TempName
NewName
PROCEDURE DIVISION.
ProcessName.
ACCEPT OldName.
UNSTRING OldName DELIMITED BY ALL SPACES
INTO TempName WITH POINTER UnstrPtr
END-UNSTRING
PERFORM UNTIL NameProcessed
STRING NameInitial "." DELIMITED BY SIZE
INTO NewName WITH POINTER StrPtr
END-STRING
UNSTRING OldName DELIMITED BY ALL SPACES
INTO TempName WITH POINTER UnstrPtr
END-UNSTRING
END-PERFORM
STRING SPACE TempName DELIMITED BY SIZE
INTO NewName WITH POINTER StrPtr
END-STRING
STOP RUN.
Tim John Roberts
R oberts
T . J . Roberts
INSPECT
INSPECT Statement Format 1
It will count the occurrence of a specific character
(alphabetic, numeric, or special character) in a data item.
INSPECT
il
SourceStr$i TALLYING
Counter# i FOR
CHARACTERS
BEFORE
AFTER
INITIAL Delim$il
ALL
LEADING
Compare
BEFORE
AFTER
INITIAL Delim$il
`
)
`
)
`
)
`
)
$
INSPECT Statement - Format 2
INSPECT SourceStr$i REPLACING
CHARACTERS BY ReplaceChar$il
BEFORE
AFTER
INITIAL Delim$il
ALL
LEADING
FIRST
Compare$il BY Replace$il
BEFORE
AFTER
INITIAL Delim$il
`
)
`
)
`
)
`
)
`
)
`
)
`
)
`
)
`
)
lim$
)
INSPECT Example 1
READ TextFile
AT END SET EndOfFile TO TRUE
END-READ
PERFORM UNTIL EndOfFile
PERFORM VARYING idx FROM 1 BY 1 UNTIL idx > 26
INSPECT TextLine TALLYING LetterCount(idx)
FOR ALL Letter(idx)
END-PERFORM
READ TextFile
AT END SET EndOfFile TO TRUE
END-READ
END-PERFORM
PERFORM VARYING idx FROM 1 BY 1 UNTIL idx > 26
DISPLAY "Letter " Letter(idx)
" occurs " LetterCount(idx) " times"
END-PERFORM
How the INSPECT works.
The INSPECT scans the Source String from left to
right counting and/or replacing characters under
the control of the TALLYING, REPLACING or
CONVERTING phrases.
The behaviour of the INSPECT is modified by using
the LEADING, FIRST, BEFORE and AFTER phrases.
An ALL, LEADING, CHARACTERS, FIRST or CONVERTING
phrase may only be followed by one BEFORE and
one AFTER phrase.
Modifying Phrases
LEADING
The LEADING phrase causes counting/replacement of all
Compare$il characters from the first valid one
encountered to the first invalid one.
FIRST
The FIRST phrase causes only the first valid character to
be replaced.
BEFORE
The BEFORE phrase designates as valid those characters
to the left of the delimiter associated with it.
AFTER
The AFTER phrase designates as valid those characters
to the right of the delimiter associated with it.
INSPECT StringData REPLACING ALL "F" BY "G"
AFTER INITIAL "A" BEFORE INITIAL "Q".
F F F F A F F F F F Q F F F Z
StringData BEFORE INSPECT
INSPECT Example 2
INSPECT Example 2
INSPECT StringData REPLACING ALL "F" BY "G"
AFTER INITIAL "A" BEFORE INITIAL "Q".
F F F F A G G G G G Q F F F Z
StringData AFTER INSPECT
F F F F A F F F F F Q F F F Z
StringData BEFORE INSPECT
INSPECT Example 3
INSPECT StringData REPLACING LEADING "F" BY "G"
AFTER INITIAL "A" BEFORE INITIAL "Z".
F F F F A F F F F F Q F F F Z
StringData BEFORE INSPECT
INSPECT Example 3
INSPECT StringData REPLACING LEADING "F" BY "G"
AFTER INITIAL "A" BEFORE INITIAL "Z".
F F F F A G G G G G Q F F F Z
F F F F A F F F F F Q F F F Z
StringData BEFORE INSPECT
StringData AFTER INSPECT
INSPECT Example 4
INSPECT StringData REPLACING ALL "F" BY "G"
AFTER INITIAL "A" BEFORE INITIAL "Z".
F F F F A F F F F F Q F F F Z
StringData
INSPECT Example 4
INSPECT StringData REPLACING ALL "F" BY "G"
AFTER INITIAL "A" BEFORE INITIAL "Z".
F F F F A G G G G G Q G G G Z
StringData
INSPECT Example 5
INSPECT StringData REPLACING FIRST "F" BY "G"
AFTER INITIAL "A" BEFORE INITIAL "Q".
F F F F A F F F F F Q F F F Z
StringData
INSPECT Example 5
INSPECT StringData REPLACING FIRST "F" BY "G"
AFTER INITIAL "A" BEFORE INITIAL "Q".
F F F F A G F F F F Q F F F Z
StringData
INSPECT Example 6
F F F F A F F F F F Q F F F Z
StringDat
a
INSPECT StringData REPLACING
ALL "FFFF" BY "FROG"
AFTER INITIAL "A" BEFORE INITIAL "Q".
INSPECT Example 6
F F F F A F R O G F Q F F F Z
StringDat
a
INSPECT StringData REPLACING
ALL "FFFF" BY "FROG"
AFTER INITIAL "A" BEFORE INITIAL "Q".
INSPECT Syntax - Format 4
INSPECT SourceStr$i CONVERTING
Compare$il TO Convert$il
BEFORE
AFTER
INITIAL Delim$il
`
)