Example: UNSTRING statement
The following example shows the UNSTRING
statement
transferring selected information from an input record. Some information
is organized for printing and some for further processing.
The FILE SECTION
defines the following
records:
* Record to be acted on by the UNSTRING statement:
01 INV-RCD.
05 CONTROL-CHARS PIC XX.
05 ITEM-INDENT PIC X(20).
05 FILLER PIC X.
05 INV-CODE PIC X(10).
05 FILLER PIC X.
05 NO-UNITS PIC 9(6).
05 FILLER PIC X.
05 PRICE-PER-M PIC 99999.
05 FILLER PIC X.
05 RTL-AMT PIC 9(6).99.
*
* UNSTRING receiving field for printed output:
01 DISPLAY-REC.
05 INV-NO PIC X(6).
05 FILLER PIC X VALUE SPACE.
05 ITEM-NAME PIC X(20).
05 FILLER PIC X VALUE SPACE.
05 DISPLAY-DOLS PIC 9(6).
*
* UNSTRING receiving field for further processing:
01 WORK-REC.
05 M-UNITS PIC 9(6).
05 FIELD-A PIC 9(6).
05 WK-PRICE REDEFINES FIELD-A PIC 9999V99.
05 INV-CLASS PIC X(3).
*
* UNSTRING statement control fields:
77 DBY-1 PIC X.
77 CTR-1 PIC S9(3).
77 CTR-2 PIC S9(3).
77 CTR-3 PIC S9(3).
77 CTR-4 PIC S9(3).
77 DLTR-1 PIC X.
77 DLTR-2 PIC X.
77 CHAR-CT PIC S9(3).
77 FLDS-FILLED PIC S9(3).
In the PROCEDURE DIVISION
, these
settings occur before the UNSTRING
statement:
- A period (
.
) is placed inDBY-1
for use as a delimiter. CHAR-CT
(thePOINTER
field) is set to 3.- The value zero (
0
) is placed inFLDS-FILLED
(theTALLYING
field). - Data is read into record
INV-RCD
, whose format is as shown below.
Here
is the UNSTRING
statement:
* Move subfields of INV-RCD to the subfields of DISPLAY-REC
* and WORK-REC:
UNSTRING INV-RCD
DELIMITED BY ALL SPACES OR "/" OR DBY-1
INTO ITEM-NAME COUNT IN CTR-1
INV-NO DELIMITER IN DLTR-1 COUNT IN CTR-2
INV-CLASS
M-UNITS COUNT IN CTR-3
FIELD-A
DISPLAY-DOLS DELIMITER IN DLTR-2 COUNT IN CTR-4
WITH POINTER CHAR-CT
TALLYING IN FLDS-FILLED
ON OVERFLOW GO TO UNSTRING-COMPLETE.
Because the POINTER
field CHAR-CT
has
value 3 before the UNSTRING
statement is performed,
the two character positions of the CONTROL-CHARS
field
in INV-RCD
are ignored.
UNSTRING results
When the UNSTRING
statement is performed,
the following steps take place:
- Positions 3 through 18 (
FOUR-PENNY-NAILS
) ofINV-RCD
are placed inITEM-NAME
, left justified in the area, and the four unused character positions are padded with spaces. The value 16 is placed inCTR-1
. - Because
ALL SPACES
is coded as a delimiter, the five contiguous space characters in positions 19 through 23 are considered to be one occurrence of the delimiter. - Positions 24 through 29 (
707890
) are placed inINV-NO
. The delimiter character slash (/
) is placed inDLTR-1
, and the value 6 is placed inCTR-2
. - Positions 31 through 33 (
BBA
) are placed inINV-CLASS
. The delimiter isSPACE
, but because no field has been defined as a receiving area for delimiters, the space in position 34 is bypassed. - Positions 35 through 40 (
475120
) are placed inM-UNITS
. The value 6 is placed inCTR-3
. The delimiter isSPACE
, but because no field has been defined as a receiving area for delimiters, the space in position 41 is bypassed. - Positions 42 through 46 (
00122
) are placed inFIELD-A
and right justified in the area. The high-order digit position is filled with a zero (0
). The delimiter isSPACE
, but because no field was defined as a receiving area for delimiters, the space in position 47 is bypassed. - Positions 48 through 53 (
000379
) are placed inDISPLAY-DOLS
. The period (.
) delimiter inDBY-1
is placed inDLTR-2
, and the value 6 is placed inCTR-4
. - Because all receiving fields have been acted on and two characters
in
INV-RCD
have not been examined, theON OVERFLOW
statement is executed. Execution of theUNSTRING
statement is completed.
After the UNSTRING
statement is performed,
the fields contain the values shown below.
Field | Value |
---|---|
DISPLAY-REC |
707890 FOUR-PENNY-NAILS 000379 |
WORK-REC |
475120000122BBA |
CHAR-CT (the POINTER field) |
55 |
FLDS-FILLED (the TALLYING field) |
6 |