MainframeMaster

COBOL Tutorial

COBOL ALPHANUMERIC-FUNCTION - Quick Reference

Progress0 of 0 lessons

Overview

ALPHANUMERIC-FUNCTION is a COBOL feature that provides functions for processing and manipulating alphanumeric data (data containing both letters and numbers). It offers various string operations and validation capabilities for working with text data, making it essential for data validation and string processing tasks.

Purpose and Usage

  • Alphanumeric data processing
  • String validation and manipulation
  • Pattern matching and checking
  • Data type validation
  • Text processing operations

Syntax

ALPHANUMERIC-FUNCTION follows specific syntax patterns for string operations:

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
24
25
* Basic ALPHANUMERIC-FUNCTION usage IF FUNCTION ALPHANUMERIC(data-field) PERFORM valid-data-processing ELSE PERFORM invalid-data-handling END-IF. * ALPHANUMERIC-FUNCTION with validation IF FUNCTION ALPHANUMERIC(customer-id) DISPLAY "Valid alphanumeric customer ID" ELSE DISPLAY "Invalid customer ID format" END-IF. * ALPHANUMERIC-FUNCTION in conditional statements EVALUATE TRUE WHEN FUNCTION ALPHANUMERIC(input-data) PERFORM process-valid-data WHEN FUNCTION ALPHABETIC(input-data) PERFORM process-alphabetic-data WHEN FUNCTION NUMERIC(input-data) PERFORM process-numeric-data WHEN OTHER PERFORM process-other-data END-EVALUATE.

ALPHANUMERIC-FUNCTION validates data containing both letters and numbers.

Data Validation Examples

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
32
33
34
35
36
* Data validation with ALPHANUMERIC-FUNCTION PROCEDURE DIVISION. VALIDATE-INPUT-DATA. * Validate customer ID (alphanumeric) IF FUNCTION ALPHANUMERIC(customer-id) PERFORM process-customer-id ELSE DISPLAY "Invalid customer ID format" PERFORM error-handling END-IF. * Validate product code IF FUNCTION ALPHANUMERIC(product-code) PERFORM process-product-code ELSE DISPLAY "Invalid product code format" PERFORM error-handling END-IF. * Validate reference number IF FUNCTION ALPHANUMERIC(reference-number) PERFORM process-reference ELSE DISPLAY "Invalid reference number format" PERFORM error-handling END-IF. PROCESS-CUSTOMER-ID. * Process valid alphanumeric customer ID DISPLAY "Processing customer ID: " customer-id. * Additional processing logic here. PROCESS-PRODUCT-CODE. * Process valid alphanumeric product code DISPLAY "Processing product code: " product-code. * Additional processing logic here.

Practical Examples

Here are some practical uses of ALPHANUMERIC-FUNCTION in COBOL:

Customer Data Validation

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
* Customer data validation with ALPHANUMERIC-FUNCTION DATA DIVISION. WORKING-STORAGE SECTION. 01 customer-data. 05 customer-id PIC X(10). 05 customer-name PIC X(50). 05 customer-code PIC X(8). 01 validation-results. 05 id-valid PIC X VALUE "N". 05 name-valid PIC X VALUE "N". 05 code-valid PIC X VALUE "N". PROCEDURE DIVISION. VALIDATE-CUSTOMER-DATA. * Validate customer ID (should be alphanumeric) IF FUNCTION ALPHANUMERIC(customer-id) MOVE "Y" TO id-valid DISPLAY "Customer ID is valid" ELSE MOVE "N" TO id-valid DISPLAY "Customer ID contains invalid characters" END-IF. * Validate customer name (should be alphabetic) IF FUNCTION ALPHABETIC(customer-name) MOVE "Y" TO name-valid DISPLAY "Customer name is valid" ELSE MOVE "N" TO name-valid DISPLAY "Customer name contains non-alphabetic characters" END-IF. * Validate customer code (should be alphanumeric) IF FUNCTION ALPHANUMERIC(customer-code) MOVE "Y" TO code-valid DISPLAY "Customer code is valid" ELSE MOVE "N" TO code-valid DISPLAY "Customer code contains invalid characters" END-IF. * Check overall validation IF id-valid = "Y" AND name-valid = "Y" AND code-valid = "Y" DISPLAY "All customer data is valid" PERFORM process-valid-customer ELSE DISPLAY "Customer data validation failed" PERFORM handle-invalid-data END-IF. PROCESS-VALID-CUSTOMER. * Process customer with valid data DISPLAY "Processing customer: " customer-id. * Additional processing logic here. HANDLE-INVALID-DATA. * Handle invalid customer data DISPLAY "Customer data validation failed". * Error handling logic here.

Comprehensive customer data validation using ALPHANUMERIC-FUNCTION.

Product Code Processing

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
* Product code processing with ALPHANUMERIC-FUNCTION PROCEDURE DIVISION. PROCESS-PRODUCT-CODES. * Process multiple product codes PERFORM UNTIL end-of-products READ product-file AT END MOVE "Y" TO end-of-products-flag NOT AT END PERFORM validate-and-process-product END-READ END-PERFORM. STOP RUN. VALIDATE-AND-PROCESS-PRODUCT. * Validate product code format IF FUNCTION ALPHANUMERIC(product-code) * Valid alphanumeric product code PERFORM process-valid-product ELSE IF FUNCTION NUMERIC(product-code) * Numeric-only product code PERFORM process-numeric-product ELSE IF FUNCTION ALPHABETIC(product-code) * Alphabetic-only product code PERFORM process-alphabetic-product ELSE * Invalid product code PERFORM handle-invalid-product-code END-IF. PROCESS-VALID-PRODUCT. * Process alphanumeric product code DISPLAY "Processing alphanumeric product: " product-code. * Additional processing logic here. PROCESS-NUMERIC-PRODUCT. * Process numeric product code DISPLAY "Processing numeric product: " product-code. * Additional processing logic here. PROCESS-ALPHABETIC-PRODUCT. * Process alphabetic product code DISPLAY "Processing alphabetic product: " product-code. * Additional processing logic here. HANDLE-INVALID-PRODUCT-CODE. * Handle invalid product code DISPLAY "Invalid product code: " product-code. * Error handling logic here.

Product code processing with different validation types.

Best Practices

  • Use ALPHANUMERIC-FUNCTION for validating mixed character data.
  • Combine with other validation functions for comprehensive data checking.
  • Provide meaningful error messages for invalid data.
  • Consider the specific requirements of your data format.
  • Test validation logic with various data scenarios.
  • Document the expected data format and validation rules.

Common Pitfalls

  • Not considering special characters in alphanumeric data.
  • Failing to handle edge cases in data validation.
  • Not providing appropriate error handling for invalid data.
  • Assuming all alphanumeric data follows the same format.
  • Not testing validation with real-world data scenarios.

Test Your Knowledge

1. What is ALPHANUMERIC-FUNCTION in COBOL?

  • A data type
  • A function for handling alphanumeric data
  • A file operation
  • A program structure

2. What type of data does ALPHANUMERIC-FUNCTION process?

  • Only numeric data
  • Only alphabetic data
  • Alphanumeric data (letters and numbers)
  • Only special characters

3. What is the primary purpose of ALPHANUMERIC-FUNCTION?

  • To perform calculations
  • To manipulate and process alphanumeric strings
  • To read files
  • To display output

4. Where is ALPHANUMERIC-FUNCTION typically used?

  • In the PROCEDURE DIVISION
  • In the DATA DIVISION
  • In the ENVIRONMENT DIVISION
  • In the WORKING-STORAGE SECTION

5. What is a common use of ALPHANUMERIC-FUNCTION?

  • File operations
  • String validation and manipulation
  • Arithmetic calculations
  • Database operations

Frequently Asked Questions