Wednesday, February 17, 2010

15. String Handling

The STRING Statement
A STRING statement may be used to combine several fields to form once concise field. This process is called concatenation.

05 NAME.
10 LAST-NAME PIC X(10) VALUE “EDISON”.
10 FIRST-NAME PIC X(10) VALUE “THOMAS”.
10 MIDDLE-NAME PIC X(10) VALUE “ALVA”.
01 NAME-OUT PIC X(33).

STRING
FIRST-NAME DELIMITED BY ‘ ‘
‘ ‘ DELIMITED BY SIZE
MIDDLE-NAME DELIMITED BY ‘ ‘
‘ ‘ DELIMITED BY SIZE
LAST-NAME DELIMITED BY ‘ ‘
‘ ‘ DELIMITED BY SIZE
INTO NAME-OUT.

Output

NAME-OUT = THOMAS ALVA EDISON

OVERFLOW Option
The OVERFLOW option specifies the operation(s) to be performed if the receiving field is not large enough to accommodate the result.

POINTER Option
We may count the number of characters actually moved to the receiving field if it is initialized at zero.

01 WS-COUNT PIC 99 VALUE 0.

MOVE 1 TO WS-COUNT.
STRING FIRST-NAME DELIMITED BY ‘ ‘
INTO NAME-OUT
WITH POINTER WS-COUNT.

Output

WS-COUNT = 6

Rules for using the STRING statement
1. The DELIMITED BY clause is required. It can indicate :
SIZE : The entire sending field is transmitted.
Literal : The transfer of data is terminated when the specified literal is encountered; the literal itself is not moved.
Identifier : The transfer of data is terminated when the contents of the identifier is encountered.
2. The receiving field must be an elementary data item with no editing symbols or JUSTIFIED RIGHT clause.
3. All literals must be described as non-numeric.
4. The identifier specified with the POINTER clause must be an elementary numeric item.
5. The STRING statement move data from left to right just like alphanumeric fields are moved, but a STRING does not pad data from left to right just like alphanumeric fields are moved, but a STRING does not pad with low-order blanks, unlike an alphanumeric MOVE.

The UNSTRING statement
The UNSTRING statement may be used to convert keyed data to a more compact form for storing it on disk. For example, we can instruct the computer to separate the NAME-OUT into its components and store them without the commas.

MOVE “THOMAS,ALVA,EDISON” TO NAME-OUT.
UNSTRING NAME-OUT
DELIMITED BY ‘,’
INTO FIRST-NAME
MIDDLE-NAME
LAST-NAME.

Output

FIRST-NAME = THOMAS
MIDDLE-NAME = ALVA
LAST-NAME = EDISON

Rules for using the UNSTRING statement
1. The sending field must be non-numeric. The receiving fields may be numeric or non-numeric.
2. Each literal must be non-numeric.
3. The [WITH POINTER identifier] and [ON OVERFLOW imperative-statement] clauses may be used in the same way as with the STRING.

The INSPECT statement
The INSPECT statement may be used for replacing a specific character in a field with another character. It can also be used for counting the number of occurrences of a given character.

01 CTR-1 PIC 9 VALUE 0.
01 WS-NAME PIC X(10).

ACCEPT WS-NAME.
INSPECT WS-NAME TALLYING CTR-1 FOR ALL SPACES.

This code will check for the number of spaces in the field WS-NAME and store the value in the field CTR-1.

Applications of the INSPECT statement
1. To count the number of occurrences of a given character in a field.
2. To replace specific occurrences of a given character with another character.


Back to COBOL Index

No comments: