MainframeMaster

COBOL Tutorial

COBOL SEPARATE Clause - Quick Reference

Progress0 of 0 lessons

Overview

The SEPARATE clause is used to split data items and perform string manipulation operations in COBOL. It provides a simple way to separate data based on delimiters and is commonly used for parsing structured text data.

Purpose and Usage

  • String splitting - Split strings based on delimiters
  • Data parsing - Parse structured text data
  • Field extraction - Extract specific fields from data
  • Data validation - Validate and format data
  • Text processing - Process text-based data

Syntax

The SEPARATE clause is used in the PROCEDURE DIVISION for string manipulation.

Basic Syntax

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
* Basic SEPARATE syntax SEPARATE source-item DELIMITER delimiter-character INTO target-item-1 target-item-2 [target-item-3...]. * Complete example IDENTIFICATION DIVISION. PROGRAM-ID. SEPARATE-EXAMPLE. DATA DIVISION. WORKING-STORAGE SECTION. 01 FULL-NAME PIC X(30). 01 FIRST-NAME PIC X(15). 01 LAST-NAME PIC X(15). 01 DELIMITER-CHAR PIC X VALUE ','. PROCEDURE DIVISION. MAIN-LOGIC. MOVE "John,Doe" TO FULL-NAME SEPARATE FULL-NAME DELIMITER DELIMITER-CHAR INTO FIRST-NAME LAST-NAME DISPLAY "First Name: " FIRST-NAME DISPLAY "Last Name: " LAST-NAME STOP RUN.

SEPARATE splits data based on delimiters.

Practical Examples

Examples of using the SEPARATE clause in different scenarios.

Name Separation

cobol
1
2
3
4
5
6
7
8
* Separate full name into components 01 FULL-NAME PIC X(40). 01 FIRST-NAME PIC X(20). 01 LAST-NAME PIC X(20). MOVE "Jane,Smith" TO FULL-NAME SEPARATE FULL-NAME DELIMITER ',' INTO FIRST-NAME LAST-NAME.

SEPARATE for name parsing.

Address Parsing

cobol
1
2
3
4
5
6
7
8
9
* Parse address components 01 ADDRESS-LINE PIC X(60). 01 STREET PIC X(30). 01 CITY PIC X(20). 01 STATE PIC X(2). MOVE "123 Main St,New York,NY" TO ADDRESS-LINE SEPARATE ADDRESS-LINE DELIMITER ',' INTO STREET CITY STATE.

SEPARATE for address parsing.

Data Validation

cobol
1
2
3
4
5
6
7
8
9
* Validate and separate data 01 INPUT-DATA PIC X(50). 01 FIELD-1 PIC X(20). 01 FIELD-2 PIC X(20). 01 FIELD-3 PIC X(10). MOVE "Value1|Value2|Value3" TO INPUT-DATA SEPARATE INPUT-DATA DELIMITER '|' INTO FIELD-1 FIELD-2 FIELD-3.

SEPARATE for data validation.

Best Practices

Understanding best practices ensures effective use of the SEPARATE clause.

Best Practices

  • Choose appropriate delimiters - Use clear, unique delimiters
  • Validate input data - Check data before separation
  • Handle edge cases - Consider missing or extra delimiters
  • Size target fields appropriately - Ensure sufficient field sizes
  • Test thoroughly - Test with various input formats

Test Your Knowledge

1. What is the primary purpose of the SEPARATE clause in COBOL?

  • To separate files
  • To separate data items and manipulate strings
  • To separate programs
  • To separate memory

2. In which COBOL division is the SEPARATE clause typically used?

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

3. What types of data can be processed using the SEPARATE clause?

  • Only numeric data
  • Only character data
  • Character and alphanumeric data
  • Only file records

4. How does SEPARATE differ from UNSTRING?

  • They are the same thing
  • SEPARATE is simpler, UNSTRING is more complex
  • SEPARATE is for output, UNSTRING is for input
  • SEPARATE is obsolete, UNSTRING is modern

5. What is the relationship between SEPARATE and DELIMITER?

  • They are unrelated
  • SEPARATE uses DELIMITER to identify separation points
  • DELIMITER is obsolete, SEPARATE is modern
  • They cannot be used together

Frequently Asked Questions