It bring you out of the dull zone when you come across things like Special-Names in cobol, otherwise English like programming language which may bore some of us. Of course we are talking about COBOL here.
Let’s create a scenario first.
Let’s assume in some requirement we need to validate an Email Address i.e. we need to make sure that the Email Address contains only the allowed characters like a-z, Numeric 0-9, Special Symbols like “@”, “_” and “.” etc.
Now one way is to split the whole string (of Email Address) into an Array and check Byte by Byte for valid values which is of course clumsy and time consuming.
A more simplistic way is to use Special-Names available in COBOL.
Special-Names allows us to declare a set of characters under a Class. Later this Class can be used to validate any string. It’s something like Condition Names however Condition Names are applicable on a fixed set of bytes but Special-Names can parse any provided string for valid characters as specified in the Class.
Let’s move straight to an example code for the scenario specified above.
IDENTIFICATION DIVISION. PROGRAM-ID. SPNames AUTHOR. KODE LEGACY. DATE-WRITTEN. 01ST JULY 2013. ENVIRONMENT DIVISION. CONFIGURATION SECTION. SOURCE-COMPUTER. IBM-370. SPECIAL-NAMES. CLASS WS-VALID-EMAIL IS '@' '_' '.' 'a' THRU 'i' 'j' THRU 'r' 's' THRU 'z' '0' THRU '9'. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-EMAIL PIC X(50). 01 WS-SPACE-CNT PIC X(2) VALUE ZEROES. PROCEDURE DIVISION. MOVE "email@example.com" TO WS-EMAIL. ******** Below INSPECT statement counts characters before initial space to exclude all Spaces ******** in 50 byte Email field as Space is not a valid character. Below reference ******** modification does it by treating (count - 1) as the length of the actual Email. INSPECT WS-EMAIL TALLYING WS-SPACE-CNT FOR CHARACTERS BEFORE INITIAL SPACE. ******** WS-VALID-EMAIL is the class declared above for valid characters. IF WS-EMAIL(1:(WS-SPACE-CNT-1)) IS NOT WS-VALID-EMAIL DISPLAY "EMAIL ADDRESS IS INVALID." ELSE DISPLAY "EMAIL ADDRESS IS VALID." END-IF. STOP RUN.
So we accumulated all the valid set of values for any character in an Email Address in Class WS-VALID-EMAIL and used it later to directly compare with the Email string which is in WS-EMAIL(1:(WS-SPACE-CNT-1)). No Byte by Byte comparison is reuired now.
That’s how Special-Names makes life this easier. Stay tuned for lot more interesting stuff in otherwise quite boring COBOL. 🙂