COBOL SPACE and SPACES - Quick Reference
SPACE and SPACES in COBOL are used to represent blank characters for data initialization, field padding, and string operations. SPACE represents a single blank character, while SPACES represents multiple blank characters.
Primary Use
Initialize data fields with blank characters
Divisions
DATA DIVISION and PROCEDURE DIVISION
Type
Literal values
Purpose
Data initialization and padding
Overview
SPACE and SPACES are COBOL literals used to represent blank characters. SPACE represents a single blank character, while SPACES represents multiple blank characters. They are commonly used for initializing character fields, padding strings to specific lengths, clearing data fields, and formatting output. These literals are essential for data processing operations that require blank character handling.
Syntax
12345678910111213* SPACE and SPACES syntax MOVE SPACE TO data-item MOVE SPACES TO data-item MOVE SPACES(n) TO data-item * n specifies number of spaces * Examples: MOVE SPACE TO WS-FIELD MOVE SPACES TO WS-STRING MOVE SPACES(10) TO WS-PADDED-FIELD * In data definitions: 01 WS-FIELD PIC X(20) VALUE SPACES. 01 WS-STRING PIC X(50) VALUE SPACE.
Practical Examples
Basic SPACE and SPACES Usage
12345678910111213141516171819202122232425262728293031323334353637* Basic SPACE and SPACES examples IDENTIFICATION DIVISION. PROGRAM-ID. SPACE-EXAMPLE. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-SINGLE-SPACE PIC X(1) VALUE SPACE. 01 WS-MULTIPLE-SPACES PIC X(10) VALUE SPACES. 01 WS-FIELD-TO-CLEAR PIC X(20). 01 WS-PADDED-STRING PIC X(30). 01 WS-FORMATTED-OUTPUT PIC X(50). PROCEDURE DIVISION. MAIN-LOGIC. * Initialize fields with spaces MOVE SPACE TO WS-SINGLE-SPACE MOVE SPACES TO WS-MULTIPLE-SPACES * Clear a field MOVE SPACES TO WS-FIELD-TO-CLEAR * Create padded string MOVE "Hello" TO WS-PADDED-STRING MOVE SPACES(25) TO WS-PADDED-STRING(6:25) * Format output with spaces MOVE "Name:" TO WS-FORMATTED-OUTPUT(1:5) MOVE SPACES(5) TO WS-FORMATTED-OUTPUT(6:5) MOVE "John Doe" TO WS-FORMATTED-OUTPUT(11:8) DISPLAY "Single space: '" WS-SINGLE-SPACE "'" DISPLAY "Multiple spaces: '" WS-MULTIPLE-SPACES "'" DISPLAY "Cleared field: '" WS-FIELD-TO-CLEAR "'" DISPLAY "Padded string: '" WS-PADDED-STRING "'" DISPLAY "Formatted output: '" WS-FORMATTED-OUTPUT "'" STOP RUN.
Explanation: This example demonstrates basic usage of SPACE and SPACES. SPACE is used to initialize a single character field, while SPACES is used for multiple characters. The program shows how to clear fields, create padded strings, and format output using blank characters.
String Padding and Formatting
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849* String padding and formatting with SPACES IDENTIFICATION DIVISION. PROGRAM-ID. STRING-PADDING-EXAMPLE. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-ORIGINAL-STRING PIC X(20) VALUE "Hello World". 01 WS-PADDED-STRING PIC X(30). 01 WS-LEFT-PADDED PIC X(25). 01 WS-RIGHT-PADDED PIC X(25). 01 WS-CENTERED-STRING PIC X(30). 01 WS-FORMATTED-REPORT PIC X(80). PROCEDURE DIVISION. MAIN-LOGIC. * Right-pad a string MOVE WS-ORIGINAL-STRING TO WS-PADDED-STRING MOVE SPACES(10) TO WS-PADDED-STRING(21:10) * Left-pad a string MOVE SPACES(10) TO WS-LEFT-PADDED(1:10) MOVE WS-ORIGINAL-STRING TO WS-LEFT-PADDED(11:11) * Right-pad a string MOVE WS-ORIGINAL-STRING TO WS-RIGHT-PADDED(1:11) MOVE SPACES(14) TO WS-RIGHT-PADDED(12:14) * Center a string MOVE SPACES(5) TO WS-CENTERED-STRING(1:5) MOVE WS-ORIGINAL-STRING TO WS-CENTERED-STRING(6:11) MOVE SPACES(5) TO WS-CENTERED-STRING(17:5) * Create formatted report line MOVE "Customer:" TO WS-FORMATTED-REPORT(1:9) MOVE SPACES(3) TO WS-FORMATTED-REPORT(10:3) MOVE "John Doe" TO WS-FORMATTED-REPORT(13:8) MOVE SPACES(5) TO WS-FORMATTED-REPORT(21:5) MOVE "Balance:" TO WS-FORMATTED-REPORT(26:8) MOVE SPACES(3) TO WS-FORMATTED-REPORT(34:3) MOVE "$1,250.00" TO WS-FORMATTED-REPORT(37:9) DISPLAY "Original: '" WS-ORIGINAL-STRING "'" DISPLAY "Right-padded: '" WS-PADDED-STRING "'" DISPLAY "Left-padded: '" WS-LEFT-PADDED "'" DISPLAY "Right-padded: '" WS-RIGHT-PADDED "'" DISPLAY "Centered: '" WS-CENTERED-STRING "'" DISPLAY "Formatted report: '" WS-FORMATTED-REPORT "'" STOP RUN.
Explanation: This example demonstrates advanced string padding and formatting techniques using SPACES. The program shows how to left-pad, right-pad, and center strings, as well as create formatted report lines. SPACES is used to create consistent spacing and alignment in output formatting.
Data Field Initialization
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354* Data field initialization with SPACE and SPACES IDENTIFICATION DIVISION. PROGRAM-ID. FIELD-INITIALIZATION-EXAMPLE. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-CUSTOMER-RECORD. 05 WS-CUSTOMER-ID PIC X(10) VALUE SPACES. 05 WS-CUSTOMER-NAME PIC X(30) VALUE SPACES. 05 WS-CUSTOMER-ADDRESS PIC X(50) VALUE SPACES. 05 WS-CUSTOMER-PHONE PIC X(15) VALUE SPACES. 05 WS-CUSTOMER-EMAIL PIC X(40) VALUE SPACES. 01 WS-TRANSACTION-RECORD. 05 WS-TRANS-ID PIC X(15) VALUE SPACES. 05 WS-TRANS-AMOUNT PIC 9(8)V99 VALUE ZERO. 05 WS-TRANS-DATE PIC X(8) VALUE SPACES. 05 WS-TRANS-TYPE PIC X(1) VALUE SPACE. 05 WS-TRANS-STATUS PIC X(1) VALUE SPACE. 01 WS-WORK-FIELDS. 05 WS-TEMP-STRING PIC X(100). 05 WS-CLEAR-FLAG PIC X(1) VALUE 'N'. PROCEDURE DIVISION. MAIN-LOGIC. * Initialize customer record with spaces MOVE SPACES TO WS-CUSTOMER-RECORD DISPLAY "Customer record initialized with spaces" * Initialize transaction record MOVE SPACES TO WS-TRANSACTION-RECORD MOVE ZERO TO WS-TRANS-AMOUNT DISPLAY "Transaction record initialized" * Clear work fields MOVE SPACES TO WS-TEMP-STRING MOVE SPACE TO WS-CLEAR-FLAG DISPLAY "Work fields cleared" * Demonstrate field-by-field initialization MOVE SPACES(10) TO WS-CUSTOMER-ID MOVE SPACES(30) TO WS-CUSTOMER-NAME MOVE SPACES(50) TO WS-CUSTOMER-ADDRESS MOVE SPACES(15) TO WS-CUSTOMER-PHONE MOVE SPACES(40) TO WS-CUSTOMER-EMAIL DISPLAY "Customer ID: '" WS-CUSTOMER-ID "'" DISPLAY "Customer Name: '" WS-CUSTOMER-NAME "'" DISPLAY "Customer Address: '" WS-CUSTOMER-ADDRESS "'" DISPLAY "Customer Phone: '" WS-CUSTOMER-PHONE "'" DISPLAY "Customer Email: '" WS-CUSTOMER-EMAIL "'" STOP RUN.
Explanation: This example shows how to use SPACE and SPACES for data field initialization. The program demonstrates initializing entire records with SPACES, clearing individual fields, and using SPACE for single-character fields. This is commonly used in data processing applications to ensure fields start with blank values.
Report Formatting Example
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667* Report formatting with SPACE and SPACES IDENTIFICATION DIVISION. PROGRAM-ID. REPORT-FORMATTING-EXAMPLE. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-REPORT-HEADER PIC X(80). 01 WS-REPORT-LINE PIC X(80). 01 WS-SEPARATOR-LINE PIC X(80). 01 WS-DETAIL-LINE PIC X(80). 01 WS-TOTAL-LINE PIC X(80). 01 WS-CUSTOMER-DATA. 05 WS-CUST-ID PIC X(10) VALUE "CUST001". 05 WS-CUST-NAME PIC X(25) VALUE "John Smith". 05 WS-CUST-BALANCE PIC 9(8)V99 VALUE 1250.50. 05 WS-CUST-STATUS PIC X(1) VALUE "A". PROCEDURE DIVISION. MAIN-LOGIC. * Create report header MOVE "CUSTOMER REPORT" TO WS-REPORT-HEADER(1:15) MOVE SPACES(10) TO WS-REPORT-HEADER(16:10) MOVE "Date: 2023-12-01" TO WS-REPORT-HEADER(26:15) MOVE SPACES(39) TO WS-REPORT-HEADER(41:39) * Create separator line MOVE SPACES(80) TO WS-SEPARATOR-LINE MOVE "=" TO WS-SEPARATOR-LINE(1:1) MOVE SPACES(78) TO WS-SEPARATOR-LINE(2:78) MOVE "=" TO WS-SEPARATOR-LINE(80:1) * Create column headers MOVE "Customer ID" TO WS-REPORT-LINE(1:11) MOVE SPACES(5) TO WS-REPORT-LINE(12:5) MOVE "Customer Name" TO WS-REPORT-LINE(17:13) MOVE SPACES(5) TO WS-REPORT-LINE(30:5) MOVE "Balance" TO WS-REPORT-LINE(35:7) MOVE SPACES(5) TO WS-REPORT-LINE(42:5) MOVE "Status" TO WS-REPORT-LINE(47:6) MOVE SPACES(27) TO WS-REPORT-LINE(53:27) * Create detail line MOVE WS-CUST-ID TO WS-DETAIL-LINE(1:10) MOVE SPACES(5) TO WS-DETAIL-LINE(11:5) MOVE WS-CUST-NAME TO WS-DETAIL-LINE(16:25) MOVE SPACES(5) TO WS-DETAIL-LINE(41:5) MOVE WS-CUST-BALANCE TO WS-DETAIL-LINE(46:10) MOVE SPACES(5) TO WS-DETAIL-LINE(56:5) MOVE WS-CUST-STATUS TO WS-DETAIL-LINE(61:1) MOVE SPACES(19) TO WS-DETAIL-LINE(62:19) * Create total line MOVE SPACES(35) TO WS-TOTAL-LINE(1:35) MOVE "Total Records: 1" TO WS-TOTAL-LINE(36:15) MOVE SPACES(29) TO WS-TOTAL-LINE(51:29) * Display formatted report DISPLAY WS-REPORT-HEADER DISPLAY WS-SEPARATOR-LINE DISPLAY WS-REPORT-LINE DISPLAY WS-SEPARATOR-LINE DISPLAY WS-DETAIL-LINE DISPLAY WS-SEPARATOR-LINE DISPLAY WS-TOTAL-LINE STOP RUN.
Explanation: This example demonstrates how to use SPACE and SPACES for report formatting. The program creates a formatted customer report with headers, separators, column alignment, and totals. SPACES is used extensively to create proper spacing and alignment in the report output, ensuring consistent formatting and readability.
Best Practices and Considerations
Important Considerations
- Use SPACE for single character fields and SPACES for multiple characters
- Consider memory usage when initializing large fields with SPACES
- Use SPACES(n) for precise control over the number of spaces
- Be consistent with spacing conventions in your application
- Consider using SPACES for data clearing and initialization
Advantages
- Simple and efficient for data initialization
- Provides consistent blank character handling
- Supports string padding and formatting
- Easy to use for field clearing
- Standard COBOL literals with wide support
Limitations
- Limited to blank character representation
- May impact memory usage for large fields
- Requires careful handling in string operations
- May not be suitable for all formatting needs
- Can be overused inappropriately
Best Practices
- • Use SPACE for single characters and SPACES for multiple characters
- • Use SPACES(n) for precise control over spacing
- • Initialize character fields with SPACES for consistency
- • Use SPACES for data clearing and formatting
- • Consider memory usage when working with large fields
Test Your Knowledge
1. What is the primary purpose of SPACE and SPACES in COBOL?
- To create blank spaces in output
- To initialize data fields with blank characters
- To add spaces between words
- To format text
2. What is the difference between SPACE and SPACES in COBOL?
- They are the same thing
- SPACE is singular, SPACES is plural
- SPACE is faster than SPACES
- SPACE is obsolete, SPACES is modern
3. In which COBOL division can SPACE and SPACES be used?
- IDENTIFICATION DIVISION only
- ENVIRONMENT DIVISION only
- DATA DIVISION and PROCEDURE DIVISION
- PROCEDURE DIVISION only
4. How are SPACE and SPACES typically used in data initialization?
- As literal values in MOVE statements
- As reserved words in data definitions
- As system variables
- As function names
5. What is the relationship between SPACE and the SPACES function?
- They are unrelated
- SPACE is a literal, SPACES is a function
- SPACE is a function, SPACES is a literal
- They are the same thing