Friday, July 18, 2008

COMPUTE Commands


The COMPUTE assigns the result of an arithmetic expression to a data-item. The arithmetic expression is evaluated according to the normal arithmetic rules. That is, the expression is normally evaluated from left to right but bracketing and the precedence rules shown below can change the order of evaluation.

Precedence Symbol Meaning
1. ** Power
2. * Multiply
/ Divide
3. + Add
- Subtract

Note that unlike some other programming languages COBOL provides the ** expression symbol to represent raising to a power.

Saturday, July 12, 2008

ACCEPT Command

The ACCEPT verb is used to get data from the keyboard, a peripheral device, or certain system variables.

ACCEPT notes
When the first format is used, the ACCEPT inserts the data typed at the keyboard (or coming from the peripheral device), into the receiving data-item.

When the second format is used, the ACCEPT inserts the data obtained from one of the system variables, into the receiving data-item.
Using the ACCEPT to get the system date and time

The second format of the ACCEPT allows the programmer to access the system date and time (i.e.the date and time held in the computer's internal clock). The system variables provided are -

* Date
* Day of the year
* Day of the week
* Time

The declarations and comments below show the format required for data-items receiving each of the system variables.

01 CurrentDate PIC 9(6).
* CurrentDate is the date in YYMMDD format

01 DayOfYear PIC 9(5).
* DayOfYear is current day in YYDDD format

01 Day0fWeek PIC 9.
* DAY-OF-WEEK is a single digit where 1=Monday

01 CurrentTime PIC 9(8).
* CurrentTime is the time in HHMMSSss format where s = S/100



New formats for the ACCEPT

The problem with ACCEPT ..FROM DATE and ACCEPT..FROM DAY is that since they hold only the year in only two digits, they are subject to the millennium bug. To resolve this problem, these two formats of now take additional (optional) formatting instructions to allow the programmer to specify that the date is to be supplied with a 4 digit year.

The syntax for these new formatting instructions is:

ACCEPT DATE [YYYYMMDD]
ACCEPT DAY [YYYYDDD]

When the new formatting instructions are used, the receiving fields must be defined as;

01 Y2KDate PIC 9(8).
* Y2KDate is the date in YYYYMMDD format

01 Y2KDayOfYear PIC 9(7).
* Y2KDayOfYear is current day in YYYYDDD format

ACCEPT and DISPLAY example program


This example program uses the ACCEPT and DISPLAY to get a student record from the user and display some of its fields. It also demonstrates how the ACCEPT can be used to get the system date and time.

$ SET SOURCEFORMAT"FREE"
IDENTIFICATION DIVISION.
PROGRAM-ID. AcceptAndDisplay.
AUTHOR. Michael Coughlan.
* Uses the ACCEPT and DISPLAY verbs to accept a student record from the user and display some of the fields. Also shows how
* the ACCEPT may be used to get the system date and time. The YYYYMMDD in "ACCEPT CurrentDate FROM DATE YYYYMMDD."
* is a format command that ensures that the date contains a 4 digit year. If not used, the year supplied by the system
* will only contain two digits which may cause a problem in the year 2000.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 StudentDetails.
02 StudentId PIC 9(7).
02 StudentName.
03 Surname PIC X(8).
03 Initials PIC XX.
02 CourseCode PIC X(4).
02 Gender PIC X.

* YYMMDD
01 CurrentDate.
02 CurrentYear PIC 9(4).
02 CurrentMonth PIC 99.
02 CurrentDay PIC 99.

* YYDDD
01 DayOfYear.
02 FILLER PIC 9(4).
02 YearDay PIC 9(3).


* HHMMSSss s = S/100
01 CurrentTime.
02 CurrentHour PIC 99.
02 CurrentMinute PIC 99.
02 FILLER PIC 9(4).


PROCEDURE DIVISION.
Begin.
DISPLAY "Enter student details using template below".
DISPLAY "Enter - ID,Surname,Initials,CourseCode,Gender"
DISPLAY "SSSSSSSNNNNNNNNIICCCCG".
ACCEPT StudentDetails.
ACCEPT CurrentDate FROM DATE YYYYMMDD.
ACCEPT DayOfYear FROM DAY YYYYDDD.
ACCEPT CurrentTime FROM TIME.
DISPLAY "Name is ", Initials SPACE Surname.
DISPLAY "Date is " CurrentDay SPACE CurrentMonth
SPACE CurrentYear.
DISPLAY "Today is day " YearDay " of the year".
DISPLAY "The time is " CurrentHour ":" CurrentMinute.
STOP RUN.


Results of running ACCEPT.CBL

Enter student details using template below
Enter - ID,Surname,Initials,CourseCode,Gender
SSSSSSSNNNNNNNNIICCCCG
9923453Power NSLM51F
Name is NS Power
Date is 01 03 1999
Today is day 060 of the year
The time is 14:41

Sunday, July 6, 2008

MOVE Command

MOVE Source$#il TO Destination$#i ...

As we can see from the syntax metalanguage above, the MOVE copies data from the source identifier or literal to one or more destination identifiers.

Although this sounds simple, the actual operation of the MOVE is somewhat more complicated and is governed by a number of rules.

MOVE rules
In most other programming languages, data is assigned from the source item on the right to the destination item on the left (e.g. Qty = 10;) but in COBOL the MOVE assigns data from left to right. The source item is on the left of the word TO and the receiving item(s) is on the right.

The source and destination identifiers can be group or elementary data-items.

When data is moved into an item, the contents of the item are completely replaced.

If the number of characters in the source data-item is less than the number in the destination item, the rest of the destination item is filled with zeros or spaces.

If the source data-item is larger than the destination item, the characters that cannot fit into the destination item will be lost. This is known as truncation.

When the destination item is alphanumeric or alphabetic (PIC X or A), data is copied into the destination area from left to right with space filling or truncation on the right.

When the destination item is numeric, or edited numeric, data is aligned along the decimal point with zero filling or truncation as necessary.

When the decimal point is not explicitly specified in either the source or destination items, the item is treated as if it had an assumed decimal point immediately after its rightmost character.

MOVE examples

MOVE num1 TO num2.
MOVE 1 TO EOFSW.
MOVE 0 TO a b c.