MainframeMaster

COBOL Tutorial

Progress0 of 0 lessons

COBOL UNSTRING and END-UNSTRING Statements

The UNSTRING statement is used for parsing and splitting strings into multiple fields.

Overview

The UNSTRING statement provides a powerful way to parse structured text data by breaking down a single string into multiple separate fields based on specified delimiters. This is particularly useful for processing formatted data such as CSV files, addresses, or other structured text input.

The END-UNSTRING statement marks the end of the UNSTRING operation and is used when additional processing is needed after the string parsing is complete.

Syntax

cobol
1
2
3
4
5
6
UNSTRING identifier-1 DELIMITED BY delimiter INTO identifier-2 identifier-3 [WITH POINTER pointer-field] [TALLYING IN tally-field] [ON OVERFLOW imperative-statement] END-UNSTRING

Basic syntax for the UNSTRING statement. The identifier-1 is the source field to parse, delimiters specify where to split the string, and identifier-2, identifier-3 are the destination fields for the parsed parts.

Parameters

  • identifier-1: The source field containing the string to parse
  • delimiter: The character(s) used to separate parts of the string
  • identifier-2, identifier-3: Destination fields for the parsed parts
  • pointer-field: Optional field to track parsing position
  • tally-field: Optional field to count parsed parts

Practical Examples

Example 1: Basic String Parsing

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
IDENTIFICATION DIVISION. PROGRAM-ID. UNSTRING-EXAMPLE. DATA DIVISION. WORKING-STORAGE SECTION. 01 INPUT-STRING PIC X(50) VALUE "John,Doe,30,Engineer". 01 FIRST-NAME PIC X(20). 01 LAST-NAME PIC X(20). 01 AGE PIC X(5). 01 OCCUPATION PIC X(20). PROCEDURE DIVISION. MAIN-PROCESS. * Parse comma-separated values UNSTRING INPUT-STRING DELIMITED BY "," INTO FIRST-NAME LAST-NAME AGE OCCUPATION END-UNSTRING DISPLAY "First Name: " FIRST-NAME DISPLAY "Last Name: " LAST-NAME DISPLAY "Age: " AGE DISPLAY "Occupation: " OCCUPATION STOP RUN.

This example demonstrates basic string parsing using UNSTRING. The input string 'John,Doe,30,Engineer' is parsed into separate fields using commas as delimiters. Each part of the string is stored in its corresponding destination field.

Example 2: Address Parsing

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
IDENTIFICATION DIVISION. PROGRAM-ID. ADDRESS-PARSER. DATA DIVISION. WORKING-STORAGE SECTION. 01 ADDRESS-STRING PIC X(100) VALUE "123 Main Street,Anytown,CA,90210". 01 STREET-ADDRESS PIC X(50). 01 CITY PIC X(30). 01 STATE PIC X(2). 01 ZIP-CODE PIC X(10). 01 PARSE-POINTER PIC 9(3). 01 PARSE-COUNT PIC 9(2). PROCEDURE DIVISION. PARSE-ADDRESS. * Parse address with pointer and tally UNSTRING ADDRESS-STRING DELIMITED BY "," INTO STREET-ADDRESS CITY STATE ZIP-CODE WITH POINTER PARSE-POINTER TALLYING IN PARSE-COUNT END-UNSTRING DISPLAY "Street: " STREET-ADDRESS DISPLAY "City: " CITY DISPLAY "State: " STATE DISPLAY "ZIP: " ZIP-CODE DISPLAY "Parts parsed: " PARSE-COUNT STOP RUN.

This example shows address parsing with additional features. The UNSTRING statement uses a pointer to track the parsing position and a tally field to count how many parts were successfully parsed. This provides useful information about the parsing operation.

Example 3: Multiple Delimiters

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
IDENTIFICATION DIVISION. PROGRAM-ID. MULTI-DELIMITER. DATA DIVISION. WORKING-STORAGE SECTION. 01 LOG-ENTRY PIC X(80) VALUE "2023-12-01 14:30:25 ERROR Database connection failed". 01 DATE-PART PIC X(10). 01 TIME-PART PIC X(8). 01 LEVEL PIC X(10). 01 MESSAGE PIC X(50). PROCEDURE DIVISION. PARSE-LOG. * Parse log entry with multiple delimiters UNSTRING LOG-ENTRY DELIMITED BY " " OR "-" OR ":" INTO DATE-PART TIME-PART LEVEL MESSAGE END-UNSTRING DISPLAY "Date: " DATE-PART DISPLAY "Time: " TIME-PART DISPLAY "Level: " LEVEL DISPLAY "Message: " MESSAGE STOP RUN.

This example demonstrates parsing with multiple delimiters. The log entry is parsed using spaces, hyphens, and colons as delimiters to extract the date, time, log level, and message components.

Example 4: Error Handling with UNSTRING

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
IDENTIFICATION DIVISION. PROGRAM-ID. UNSTRING-ERROR-HANDLING. DATA DIVISION. WORKING-STORAGE SECTION. 01 INPUT-DATA PIC X(50). 01 FIELD-1 PIC X(20). 01 FIELD-2 PIC X(20). 01 FIELD-3 PIC X(20). 01 PARSE-STATUS PIC X(1). PROCEDURE DIVISION. PROCESS-INPUT. DISPLAY "Enter comma-separated data: " ACCEPT INPUT-DATA * Parse with overflow handling UNSTRING INPUT-DATA DELIMITED BY "," INTO FIELD-1 FIELD-2 FIELD-3 ON OVERFLOW MOVE "E" TO PARSE-STATUS DISPLAY "Error: Too many fields to parse" END-UNSTRING IF PARSE-STATUS NOT = "E" DISPLAY "Field 1: " FIELD-1 DISPLAY "Field 2: " FIELD-2 DISPLAY "Field 3: " FIELD-3 END-IF STOP RUN.

This example shows error handling with UNSTRING. The ON OVERFLOW clause handles cases where there are more parts in the input string than destination fields available. This prevents program errors when parsing unpredictable input data.

Best Practices and Considerations

Best Practices

  • Always use END-UNSTRING to properly close the UNSTRING operation
  • Use appropriate field sizes for destination fields
  • Implement error handling with ON OVERFLOW
  • Use POINTER and TALLYING for complex parsing operations
  • Test UNSTRING with various input formats

Considerations

  • Field length compatibility between source and destination
  • Delimiter selection and handling
  • Performance implications for large strings
  • Error handling and recovery procedures
  • Data validation after parsing

Test Your Knowledge

1. What is the primary purpose of the UNSTRING statement in COBOL?

  • To combine multiple strings
  • To parse and split strings into multiple fields
  • To convert strings to numbers
  • To format strings

2. In which COBOL division is the UNSTRING statement typically used?

  • IDENTIFICATION DIVISION
  • ENVIRONMENT DIVISION
  • DATA DIVISION
  • PROCEDURE DIVISION

3. What is the relationship between UNSTRING and delimiters?

  • They are unrelated
  • UNSTRING uses delimiters to split strings
  • Delimiters use UNSTRING
  • They are the same thing

4. What are the main components of an UNSTRING statement?

  • Source field only
  • Source field, destination fields, and delimiters
  • Only destination fields
  • Only delimiters

5. When should you use the UNSTRING statement?

  • Only for numeric data
  • For parsing structured text data into separate fields
  • Only for file operations
  • Only for display purposes

Frequently Asked Questions