Sunday, September 26, 2010

COBOL Tutorial 000200 – Data Fields

Variables are called Fields in COBOL and definitions of variables are declared in the Picture clause (can be abbreviated with PIC). Why is it called the Picture Clause? According to the book Sams Teach Yourself COBOL in 24 Hours, this is because it “paints a picture of how a field looks by defining every details and characteristic of the field”, still doesn’t quite make sense to me but anyway.

Let’s start by talking about what data fields (variables) look like in C#. When we declare a variable, the first thing we have to think about is the data type, which determines what kind of data can it hold. Normally we wouldn’t worry about the number of digits or length of the string unless we know their values can get ridiculously large or long.

int integerVariable = 12345678;
string stringVariable = "abcd1234";
decimal decimalVariable = 1234.5678m;

In a COBOL world, however, the size does matter and you have to specify both the type and the size for each data field at the same time with a special character mask in its “Picture Clause” (the PIC keyword) as shown in code below:

01 ALPHA-FIELD         PIC AAAAAAAAAA.
01 NUMERIC-FIELD       PIC 9999999999.
01 ALPHA-NUMERIC-FIELD PIC XXXXXXXXXX.

There are really only 3 types of data field in COBOL: literal, numeric and alpha-numeric and as their names suggest, they hold alphabetical, numeric and alpha-numeric characters respectively.

Let’s look at the ALPHA-FIELD first, its picture clause specifies a “AAAAAAAAAA” mask, each “A” character is a place holder for a single alphabetical character, so the ALPHA-FIELD can be used to store 10 alphabetical characters. Similarly, each “9” is a place holder for a single digit and a “X” is a place holder for a single alpha-numeric character. Therefore, the NUMERIC-FIELD and ALPHA-NUMERIC-FIELD can hold 10 digits and 10 alpha-numeric characters respectively.

To declare a decimal data field, we need to add the special character “V” in the numeric data field mask, which specifies the decimal point location. For example, the DECIMAL-NUMERIC-FIELD data field declared below let you store decimal values with up to 5 digits before and after the decimal point.

01 DECIMAL-NUMERIC-FIELD PIC 99999V99999.

As you have probably noticed, the format mask can get very ugly for large fields and hence COBOL allows you to abbreviate it with the special character followed by the number of appearances in round brackets, so for example we can abbreviate “XXXXXXXXXX” to X(10) and “99999V99999” to 9(5)V9(5).

Let’s put these all together into a small program:

       IDENTIFICATION DIVISION.
       PROGRAM-ID. EDITED.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 ALPHA-FIELD PIC A(10).
       01 NUMERIC-FIELD PIC 9(10).
       01 ALPHA-NUMERIC-FIELD PIC X(10).
       01 DECIMAL-NUMERIC-FIELD PIC 9(5)V9(5).

       PROCEDURE DIVISION.
            MOVE 'ABCEFG' TO ALPHA-FIELD
            DISPLAY '         ALPHA-FIELD: ' ALPHA-FIELD.

            MOVE 123456 TO NUMERIC-FIELD.
            DISPLAY '       NUMERIC-FIELD: ' NUMERIC-FIELD.

            MOVE 'ABC123' TO ALPHA-NUMERIC-FIELD
            DISPLAY ' ALPHA-NUMERIC-FIELD: ' ALPHA-NUMERIC-FIELD.


            MOVE 1234.5 TO DECIMAL-NUMERIC-FIELD.
            DISPLAY 'EDITED-NUMERIC-FIELD: ' DECIMAL-NUMERIC-FIELD.
            
            STOP RUN.

Notice that I’ve used the abbreviated picture clause mask (e.g. X(10) instead of XXXXXXXXXX) in the example above. Also, we haven’t covered the MOVE command yet but basically that’s how you move (assign) values into data fields in COBOL. If you compile and run this program you’ll see the following outputs in the console:

         ALPHA-FIELD: ABCEFG    
       NUMERIC-FIELD: 0000123456
 ALPHA-NUMERIC-FIELD: ABC123    
EDITED-NUMERIC-FIELD: 01234.50000

As you have probably noticed, unused digits in numeric fields are padded with zeros, which is quite ugly. We’ll cover how to make it look prettier with Edited Fields in the next tutorial.

Update: This is the 3rd revision of this COBOL data fields tutorial, I’ve decided to cut this tutorial into two parts because it was getting too long and messy.

2 comments:

  1. Thank you for this Oscar. Another tips for me. ;)
    Keep posting like this.

    Anatoly
    -Algozone

    ReplyDelete
  2. Thanks man, ur awesome!! :D

    ReplyDelete