Saturday, December 6, 2008

Handling an empty input file

When reading a sequential file, I always use the AT END clause. For example:

READ STUDENT-FILE
     AT END MOVE 1 TO END-OF-FILE
END READ

So that if an end of file is reached, END-OF-FILE data field will have a value of '1' and I will use this value to check if an end of file is reached. It also can be used to check for an empty file. It looks like this:

IF END-OF-FILE = 1
     CLOSE STUDENT-FILE
     STOP RUN
END-IF

After the READ statement, before processing the records in the file, I always check if I'm reading an empty file so as to avoid errors and also to save time by ending the program immediately after it satisfies the condition 'IF END-OF-FILE = 1'.

Sunday, October 19, 2008

Initialize statement

I find it very important to initialize first data fields that are to be used in the program to make certain that it will not contain any trash. INITIALIZE statement is like MOVE statement. It is like you are moving spaces to alphanumeric data fields and zeroes to numeric data fields. Consider the following example:

01 WS-EMPLOYEE-RECORD.
     05 WS-NAME          PIC X(20).
     05 WS-BIRTHDAY    PIC 9(8).
     05 WS-ADDRESS     PIC X(30).
     05 WS-RATE          PIC 9(4)V99.
     05 WS-HOURS        PIC 9(4)99.

INITIALIZE WS-EMPLOYEE-RECORD.

The above statement will move spaces to WS-NAME and WS-ADDRESS while zeroes will be moved to WS-BIRTHDAY, WS-RATE and WS-HOURS.

Aside from INITIALIZE statement, VALUE clause and MOVE statement can also be used to initialize a data field.

If the value of a data field will not change until the end of the program, use the VALUE clause to lessen the execution time.

01 WS-RATE PIC 9(4)V99 VALUE 0100.50.

Use MOVE statement if a data field will continually change.

MOVE 12251979 TO WS-BIRTHDAY.

According to the book 'Structured COBOL First Course' by M. B. Khan, less execution time for VALUE clause for it assigns the value during compilation whereas the MOVE statement assigns it during execution time.

By experience, if I'm not moving anything to a data field using MOVE statement, VALUE clause and INITIALIZE statement it will contain LOW-VALUES.

Friday, October 17, 2008

Data field with PIC 9(18) PICTURE clause

I was about to turnover in production the COBOL program that I've revised due to some discrepancies. It's quite a habit for me to check first the output of the program before passing it for implementation. And this is what I've learned...

Consider the following example:

01 EMPLOYEE-RECORD.
     10 EMP-ID-CODE PIC 9(02).
     10 EMP-BDAY PIC 9(08).
     10 EMPLOYEE-NO PIC 9(10).

01 WS-EMP-NBR PIC 9(18).

01 WS-EMP-RECORD PIC X(20).

I don't need the EMP-ID-CODE. Just their birthday and employee number. So I

MOVE EMPLOYEE-RECORD TO WS-EMP-NBR.

Thinking that it will eliminate EMP-ID-CODE since WS-EMP-NBR is shorter than the value being moved and should cut off the value from the left since the receiving data field is defined as numeric.

If EMPLOYEE-RECORD contains

.

And I was expecting after the move statement to be

.

But instead

were the contents.

To sum it all up, avoid moving group data field, like the one above, in numeric data field for it will not truncate the value from the left as I have expected. Move the group data field first into elementary data field, same as the solution that I did which is also shown below, to come up with just the employee's birthday and employee number. Also, always check the output if it's what you really want the outcome to be. So below is what I did;

MOVE EMPLOYEE-RECORD TO WS-EMP-RECORD.
MOVE WS-EMP-RECORD TO WS-EMP-NBR.

Just also want to add that PIC 9(18) is the maximum allowable size for numeric data field. In excess of this will cause an error.


Monday, October 6, 2008

HIGH-VALUES

While I was in the middle of revising a program, I tried to move HIGH-VALUES to a data field

MOVE HIGH-VALUES TO WS-RECEIVING FIELD

where

01 WS-RECEIVING FIELD PIC X(10) is in the WORKING-STORAGE SECTION.

Contents of WS-RECEIVING FIELD after the MOVE



I have to type "hex on" on the command line to make sure that it is not spaces.



Actually both LOW-VALUES and HIGH-VALUES look like SPACES when "hex off". The difference is that SPACES hex value is:



Saturday, October 4, 2008

Dealing with abends - S0C7

I have recently been part of a project. Thank God that it is now implemented and being used by our clients. I have encountered a data exception error (abend code = S0C7) during the pilot implementation. It took me and my teammates 2 hours to correct this error. Because of this experience I should be careful next time not to move alphabetic character or spaces to a data field that is declared numeric for it will cause a data exception error or S0C7.

Meanings of Figurative Constants

I constantly use figurative constants in assigning initial values to data fields thus it is helpful to know what are these figurative constants and its meaning.

Figurative Constant     Meaning

SPACE(S)                    Fills the field with blank spaces
ZERO(S)                      Fills the field with zeroes
HIGH-VALUE(S)         Fills the field with the highest binary value the computer system being used is able to represent
LOW-VALUE(S)          Fills the field with the lowest binary value the computer system being used is able to represent


Figurative contants by the way are reserved words in COBOL. These have predefined meanings to the compiler.

Reference:
(1) Khan, M.B.,
Structured COBOL: First Course
, 1996, boyd & fraser publishing company.



Moving LOW-VALUES to a data-field

I am a Cobol Programmer. I have been in this career since 2004. I am a licensed ECE but my heart is really with programming. So I'm writing to give you some pointers that will somehow help you. When I was in my early years in this job, I had so many things that I wanted to know like what will be the contents of the receiving data field when I move LOW-VALUE(S) to it.

For example in my WORKING-STORAGE SECTION, I will have

01 WS-RECEIVING-FIELD PIC X(10)

and in PROCEDURE DIVISION, I will code

MOVE LOW-VALUES TO WS-RECEIVING-FIELD.

The contents of the data field WS-RECEIVING-FIELD after executing the previous MOVE statement will be




when "hex on" is typed in the command line.