P. 1
COBOL - Programming Guide

COBOL - Programming Guide

|Views: 968|Likes:
Publicado pormukesh

More info:

Published by: mukesh on Jul 01, 2011
Direitos Autorais:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
See more
See less

01/08/2015

You can manipulate pointer data items, which are a special type of data item to
hold addresses, when you want to pass and receive addresses of a variably
located record area.Pointer data items are data items that are either explicitly
defined with the USAGE IS POINTER clause, or are ADDRESS special registers.
A typical application for using pointer data items is in processing a chained list (a
series of records where each one points to the next).

For this example, picture a chained list of data that contains individual salary
records.Figure88 shows one way to visualize how these records are linked in
storage:

┌─────────┐

┌──────────┐

SALARYRECORD

┌───────────┴─┐

┌──────────┴─┐

┌─────────────┐

PTR─NEXT─REC │addrofnext│

│NULLinvalid│

│rec

│addr

├─────────────┤

├────────────┤ . . . ├─────────────┤

NAME

├─────────────┤

├────────────┤

├─────────────┤

SALARY

└─────────────┘

└────────────┘

└─────────────┘

Figure 88. RepresentationofaChainedListEndingwithNULL

The first item in each record points to the next record, except for the last record.
The first item in the last record, in order to indicate that it is the last record, con-
tains a null value instead of an address.

274COBOL/VSE Programming Guide

Advanced Topics

The high-level logic of an application that processes these records might look
something like this:

OBTAIN ADDRESS OF FIRST RECORD IN CHAINED LIST FROM ROUTINE
CHECK FOR END OF THE CHAINED LIST
DO UNTIL END OF THE CHAINED LIST
PROCESSRECORD
GO ON TO THE NEXT RECORD

END

Figure89 contains an outline of the processing program, LISTS, used in this
example of processing a chained list.

IDENTIFICATIONDIVISION.
PROGRAM-ID.LISTS.
ENVIRONMENTDIVISION.
DATADIVISION.
ᑍᑍᑍᑍᑍᑍ

WORKING-STORAGESECTION.
77PTR-FIRST

POINTERVALUE IS NULL.

77DEPT-TOTAL

PIC 9(4) VALUE IS .

ᑍᑍᑍᑍᑍᑍ
LINKAGESECTION.
1SALARY-REC.
2PTR-NEXT-REC

POINTER.

2NAME

PICX(2 ).

2DEPT

PIC9(4).

2SALARY

PIC9(6).

1DEPT-X

PIC9(4).

ᑍᑍᑍᑍᑍᑍ

PROCEDURE DIVISION USING DEPT-X.

ᑍᑍᑍᑍᑍᑍ
ᑍ FOR EVERYONE IN THE DEPARTMENT RECEIVED AS DEPT-X,
ᑍ GO THROUGH ALL THE RECORDS IN THE CHAINED LIST BASED ON THE
ᑍ ADDRESS OBTAINED FROM THE PROGRAM CHAIN-ANCH
ᑍ AND CUMULATE THE SALARIES.
ᑍ IN EACH RECORD, PTR-NEXT-REC IS A POINTER TO THE NEXT RECORD
ᑍ IN THE LIST; IN THE LAST RECORD, PTR-NEXT-REC IS NULL.
ᑍ DISPLAY THE TOTAL.
ᑍᑍᑍᑍᑍᑍ

CALL "CHAIN-ANCH" USING PTR-FIRST
SET ADDRESS OF SALARY-REC TO PTR-FIRST

ᑍᑍᑍᑍᑍᑍ

PERFORM WITH TEST BEFORE UNTIL ADDRESS OF SALARY-REC = NULL
IF DEPT = DEPT-X
THEN ADD SALARY TO DEPT-TOTAL
ELSECONTINUE

END-IF
SET ADDRESS OF SALARY-REC TO PTR-NEXT-REC
END-PERFORM

ᑍᑍᑍᑍᑍᑍ

DISPLAYDEPT-TOTAL
GOBACK.

Figure 89. ProgramforProcessingaChainedList

Passing Addresses between Programs

To obtain the address of the first SALARY-REC record area, program LISTS calls
program CHAIN-ANCH:

CALL "CHAIN-ANCH" USING PTR-FIRST

PTR-FIRST is defined in WORKING-STORAGE in the calling program (LISTS) as a
pointer data item:

Chapter 16.Subprograms and Data Sharing

275

Advanced Topics

WORKING-STORAGESECTION.
1PTR-FIRST

POINTERVALUE IS NULL.

Upon return from the call to CHAIN-ANCH, PTR-FIRST contains the address of the
first record in the chained list.

PTR-FIRST is initially defined as having a NULL value as a logic check.If some-
thing goes amiss with the call, and PTR-FIRST never receives the value of the
address of the first record in the chain, a NULL value remains in PTR-FIRST and,
according to the logic of the program, the records will not be processed.

NULL is a figurative constant used to assign the value of an invalid address (non-
numeric 0) to pointer items.It can be used in the VALUE IS NULL clause, in the
SET statement, and as one of the operands of a relation condition with a pointer
data item.

The LINKAGE SECTION of the calling program contains the description of the
records in the chained list.It also contains the description of the department code
that is passed, using the USING clause of the CALL statement.

LINKAGESECTION.
1SALARY-REC.
2PTR-NEXT-REC

POINTER.

2NAME

PICX(2 ).

2DEPT

PIC9(4).

2SALARY

PIC9(6).

1DEPT-X

PIC9(4).

To “base” the record description SALARY-REC on the address contained in
PTR-FIRST, use a SET statement:

CALL "CHAIN-ANCH" USING PTR-FIRST
SET ADDRESS OF SALARY-REC TO PTR-FIRST

Checking for the End of the Chained List

The chained list in this example is set up so the last record contains an invalid
address.To do this, the pointer data item in the last record would be assigned the
value NULL.

A pointer data item can be assigned the value NULL in two ways:

A pointer data item can be defined with a VALUE IS NULL clause in its data

definition.

NULL can be the sending field in a SET statement.

In the case of a chained list in which the pointer data item in the last record con-
tains a NULL value, the code to check for the end of the list would be:

IF PTR-NEXT-REC = NULL
.
.
.

(logic for end of chain)

If you have not reached the end of the list, process the record and move on to the
next record.

276COBOL/VSE Programming Guide

Advanced Topics

In the program LISTS, this check for the end of the chained list is accomplished
with a “DO WHILE” structure:

PERFORM WITH TEST BEFORE UNTIL ADDRESS OF SALARY-REC = NULL
IF DEPT = DEPT-X
THEN ADD SALARY TO DEPT-TOTAL
ELSECONTINUE

END-IF
SET ADDRESS OF SALARY-REC TO PTR-NEXT-REC

END-PERFORM

Continuing Processing the Next Record

To move on to the next record, set the address of the record in the
LINKAGE-SECTION to be equal to the address of the next record.This is accom-
plished through the pointer data item sent as the first field in SALARY-REC:

SET ADDRESS OF SALARY-REC TO PTR-NEXT-REC

Then repeat the record-processing routine, which will process the next record in the
chained list.

A Variation:Incrementing Addresses Received from Another
Program

The data passed from a calling program might contain header information that you
want to ignore (for example, in data received from a CICS application that is not
migrated to the command level).

Because pointer data items are not numeric, you cannot directly perform arithmetic
on them.However, you can use the SET verb to increment the passed address in
order to bypass header information.

You could set up the LINKAGE-SECTION like this:

LINKAGESECTION.
1RECORD-A.
2HEADER

PICX(12).
2REAL-SALARY-REC PIC X(3 ).

.
.
.
1SALARY-REC.
2PTR-NEXT-REC

POINTER.

2NAME

PICX(2 ).

2DEPT

PIC9(4).

2SALARY

PIC9(6).

Within the Procedure Division, “base” the address of SALARY-REC on the address
of REAL-SALARY-REC:

SET ADDRESS OF SALARY-REC TO ADDRESS OF REAL-SALARY-REC

SALARY-REC is now based on the address of RECORD-A + 12.

Chapter 16.Subprograms and Data Sharing

277

Advanced Topics

You're Reading a Free Preview

Descarregar
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->