Skip to main content

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.

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

Post a Comment

Popular posts from this blog

Load Testing ASP.NET Sites with JMeter

Following my previous post about using JMeter to test MOSS, I tried to figure out what are the bare minimum requirements of using JMeter against a plain ASP.NET website. I wrote a very simple ASP.NET web application with just a button, a text fields and a static label. This application displays the content of a text file in the static label when it loads and write content of the text field back to the file when the button is clicked.I found all I need to do in order to script this using JMeter is to extract __VIEWSTATE and __EVENTVALIDATION fields then send them back in the update request. My JMeter test plain looks like this:

Load Testing SharePoint (MOSS) Sites with JMeter

I have used JMeter for load testing few non-ASP.NET web sites before, however I could not get it to work with ASP.NET web sites. This is mainly due to ASP.NET ViewState and event validations, which stops a recorded JMeter script from being played back.Recently I worked on a MOSS project and we were looking for tools to perform load testing on the server. Many people said the load testing tool in Microsoft Team System for Testers works well with MOSS. However, it is quite expensive so I decided to give JMeter another go. After several hours of hacking, I actually got it to work and here’s how I did it.My test page is the pretty standard MOSS edit document property screen with few extra text fields added and the goal here is to use a JMeter script to change the document properties. Once I have a working script, I can configure JMeter to fire hundreds of instances of this script simultaneously to simulate the user workload.As shown in the screenshot below, the test plan contains two HTTP…

Getting HP Mini 1000 Wireless to Work Under Ubuntu 9.10 Karmic Koala‎ Netbook Remix

I installed Ubuntu 9.10 Netbook Remix in my (actually my wife's) HP mini 1000 this afternoon. To my surprise the wireless card did not work. Also, when I looked at System -> Administration -> Hardware Drivers, the list was blank.After few hours of googling and reading through several not too helpful forum posts, I learned that this was caused by Ubuntu 9.10 shipping "b43" driver out of box, which does not work for HP mini 1000. The proprietary driver "wl" should be used instead. However, no one said exactly what I needed to do to fix this problem.Eventually, I decided to just launch Synaptic and search for "broadcom". The first result in the filtered list was bcmwl-kernel-source, which looked promising so I just went ahead and installed it.I had a look at the /etc/modprobe.d folder after the installation finished, I noticed that the package actually created a blacklist file for "b43" related modules for me already. After reboot, my wir…