multi-dimensional arrays. time table periodmondaytuesdaywedthursfri 5bac344aibc233bstudent help...

Post on 26-Dec-2015

214 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Multi-Dimensional Arrays

Time Table

Period Monday Tuesday Wed Thurs Fri

5 BAC344A IBC233B Student Help

6 BAC344A IBC233B StudentHelp

7 IBC233A Student Help

IBC233AB

8 IBC233A StudentHelp

IBC233AB

9 IBC233C IBC233D IBC233CD

10 IBC233C IBC233D IBC233CD

11 BAC344A

12 BAC344A

Time table arrayarranged by row

01 Time-table-array.

05 Time-entry occurs 12 times. 10 Mon-Course-Code PIC X(8).

10 Tues-Course-Code PIC X(8).

10 Wed-Course-Code PIC X(8).

10 Thurs-Course-Code PIC X(8).

10 Fri-Course-Code PIC X(8).

Time table arrayarranged by column

01 Time-table-array.

05 Time-entry occurs 5 times. 10 PRD1-Course-Code PIC

X(8).

10 PRD2-Course-Code PIC X(8).

10 PRD3-Course-Code PIC X(8). ...

10 PRD12-Course-Code PIC X(8).

2 dimensional Time table array“all in one”

01 Time-table-array.

05 Day-in-week occurs 5 times.

10 Course-entry occurs 12 times.

15 Period-Number PIC 9(2).

15 Course-Code PIC X(8).

Load a Multi-Dimensional Arrayusing the

PROCEDURE DIVISION

Move ‘IBC233B’ to Course-Code (2, 5)

Move 5 to Period-Number (2, 5)

Move ‘IBC233B’ to Course-Code (2, 6)

Move 6 to Period-Number (2, 6)

Move ‘Student Help’ to Course-Code (2, 7)

Move 7 to Period-Number (2, 7)

Processing a Multi-Dimensional Array”THE LONG WAY”

Example counts the number of Student Help Time periods in a week.

Perform 330-Count-Office Varying Day-Sub from 1 by 1 until Day-Sub > 5.

330-Count-Office.Perform 335-Count-Office-2 Varying Period-Sub from 1 by until Period-Sub > 12.

335-Count-Office-2.If Course-Code (Day-Sub, Period-Sub) = ‘Student Help’

Add 1 to office-count.

Processing a Multi-Dimensional ArrayThe “smart way”

Example counts the number of Student Help Time Periods in a week.

Perform 430-Count-Office Varying Day-Sub from 1 by 1 until Day-Sub > 5After Period-Sub from 1 by 1 until Period-Sub >

12.

430-Count-Office.If Course-Code (Day-Sub, Period-Sub) = ‘Student

Help’Add 1 to office-count.

Searching a Multi-Dimensional Array01 Time-table-array.

05 Day-in-week occurs 5 times indexed by Day-Sub, 10 Course-entry occurs 12 times indexed by

Period-Sub. 15 Period-Number PIC 9(2). 15 Course-Code PIC X(8).

77 Match-Found PIC X(3).77 Day-Found PIC 9(1).77 Period-Found PIC 9(2).

Note: indexes defined by the system

Searching a Multi-Dimensional Array

Find the first occurrence of the course code IBC233AB.

Move ‘No’ to Match-Found.Perform 500-Search-It Varying Day-Sub from 1 by 1

until Day-Sub > 5 or Match-Found = ‘Yes’.500-Search-It.

Set Period-Sub to 1.Search Course-Entry

When Course-Code (Day-Sub, Period-Sub) = ‘IBC233AB’Move Day-Sub to Day-FoundMove Period-Sub to Period-FoundMove ‘Yes’ to Match-Found

END-Search.

Structured Programming

Series of Independent Modules executed from a main module.

Structured Programming

• Logical program design

• Avoid duplication of code– Improves programmer productivity

• Greater Flexibility– Modules/programs can be written in languages

best suited for the task

• End up with greater standardization

• Intercommunicating Programs– Copy Statement– Structured Programming (Call Statement)

Copy (Source)anywhere in a program and optionally substituting variables

COPY member-name/record-name OF/IN Library-file

REPLACING

{pseudo-text1/identifier-1/literal-1/word-1}

BY{pseudo-text2/identifier-2/literal-2/word-2}

Copy external file structures

COPY DDS-format-name

DDS-ALL-FORMATSDDS uses the externally declared field names

COPY DD-format-name

DD-ALL-FORMATSDD uses the alias names used in external declarations

Calling programsand

passing parameters(values)

OPM - DYNAMIC CRTCBLPGM *PGM option 14 on a CBL source member

ILE - STATICCRTCBLMOD *MODULE option 15 on a CBLLE member

CRTPGM *PGM command line CRTPGM F4

CRTBNDPGM *PGM option 14 on a CBLLE member

Static vs Dynamic CallsOPM – Original Program Model

Dynamic *PGM to *PGM calls

Called program loaded into memory and executed at runtime

ILE – Integrated Language EnvironmentStatic modules (*MODULE) ARE bound at compile time (crtcblmod).*PGM object created by CRTPGM command

Called MODULES are loaded together at the same time before any execution by a CALL(faster execution / promotes modular

programming / mix and match language object execution)

19

OPM Benefits• Benefits

– Calls are resolved a runtime. Called programs do not have to exist at the time that the calling program is compiled

– Calling programs do not have to be recompiled when a called program changes (as long as the parameters stay the same.

– Good use of AS/400 Disk Space– Version control

20

OPM Disadvantages

• Many calls leads to performance problems

• Compatibility problems between two languages.

21

Integrated Language Environment

• (we’ve been using OPM - Original Program Model)

22

ILE• Uses Static Calls

• Modules are copied in to the program object when the program is bound together.

• Steps

– Each program is compiled into a module

– Modules are bound together using ‘Bind by copy’.

http://public.boulder.ibm.com/pubs/html/as400/online/v4r5eng.htm

ILE COBOL for AS/400 programmer guide SC09-2540-01.

ILE COBOL for AS/400 reference SC09-2539-01

http://public.boulder.ibm.com/pubs/html/as400/online/v4r4eng.htm

ILE Concepts V4R4 SC41-5606-03

Programming Changes for ILE

• To call another program– CALL LINKAGE TYPE IS PROGRAM

‘program-name’

• To call another procedure (*MODULE)– CALL LINKAGE TYPE IS PROCEDURE

‘program-name’

Program Creation Steps

• Type in the source code into source members with the type CBLLE

• Create procedures (*MODULES) from the source code using the CRTCBLMOD command

• Create a program object from all of the *MODULES using the CRTPGM command

Combining Modules

Example

MODA *MODULE calls

MODB *MODULE

To create a program call MODCALL:CRTPGM MODCALL MODULES(MODA MODB)

26

ILE Benefits

• Resolves language incompatibility issues

• Less Call intensive– All program modules are opened at the same

time

27

ILE Disadvantages

• Uses more disk space.

• All of the modules must exist before the final product can be bound.

• Version control. (none)

28

Program Creation Steps - Short Cut (we have been using this so far)

If the program consists of one module, then compile the module and create the program using the CRTBNDCBL command.

(option 14 against a CBLLE source member)

It Combines the CRTCBLMOD and CRTPGM functions to produce the *PGM object but DOES NOT RETAIN the modules!

29

Program Creation Steps• Use SEU to type in the source code into source

members with the type CBLLE (QCBLLESRC)

• Create procedures from the source code using the CRTCBLMOD command (type *MODULES CBLLE)

• Create a program object from all of the *MODULES using the CRTPGM command (type *PGM CBLLE)

30

Display Program Information Display 3 of 7 Program . . . . . . . : ASSIGN2 Library . . . . . . . : CBL344LIB Owner . . . . . . . . : MOOGK Program attribute . . : CBLLE Detail . . . . . . . . : *MODULE Type options, press Enter. 5=Display description 6=Print description Creation Optimization Debug Opt Module Library Attribute Date Level Data __ VM200C MOOGK CBLLE 06/20/01 *NONE *YES __ PR200C MOOGK CBLLE 06/20/01 *NONE *YES

Opt Object Type Attribute Text 5_ ASSIGN2 *PGM CBLLE Fall 00 Assignment 2

31

Executable type *PGM created from several modules of type *MODULE

*MODULE

*MODULE

*MODULE

MULTI MODULE MODULE PEP!

*module

*module*module

Calling ProgramsThe program being called

• Linkage Section– Part of the Data Division, usually coded after

the working-storage section.– Defines the parameters being sent to the called

program.– Only needed if parameters being passed to the

program

Calling ProgramsThe program being called

• Procedure Division Statement– Using clause– Only needed if parameters are passed to calling

program.• Example:

Linkage Section.

01 PARM-Customer PIC X(10).

Procedure Division using PARM-Customer.

Calling ProgramsProgram being called

• Must end with EXIT PROGRAM instead of STOP RUN.

• Because … STOP RUN terminates the entire process / application

Calling ProgramsThe program doing the calls

Syntax:CALL LINKAGE TYPE IS

PROCEDURE/PROGRAM ‘programname’ USING parameter list

END-CALL.

PROCEDURE is used if the object being called is a *MODULE.

PROGRAM is used if the object being called is a *PGM.

USING is used only if parameters are passed.

CALL Statement with Parameters

Data Division. 01 FLDA PIC X(2). 01 FLDB PIC 9(5).

Procedure Division. CALL Linkage type is program PGMB USING FLDA FLDB.

Identification DivisionPROGRAM-ID. PGMB.

Data Division.

Linkage Section.01 FLDX PIC X(2).01 FLDY PIC 9(5).

Procedure Division USING FLDX FLDY.

Calling Program

Called Program

CALL Statement with Parameters (OPM)

Data Division.01 05 FLDA PIC X(2). 05 FLDB PIC 9(5).

Procedure Division.

CALL PGMB USING FLDA FLDB.

Identification DivisionPROGRAM-ID. PGMB.

Data Division.

Linkage Section.01 FLDX PIC X(2).01 FLDY PIC 9(5).

Procedure Division USING FLDX FLDY.

Calling Program

Called Program

WORKING-STORAGE SECTION.….. 01 ARG-LIST. 05 PARTCODE PIC A. 05 PARTNO PIC X(4). 05 U-SALES PIC 9(5). . . .PROCEDURE DIVISION. . . . CALL PGMB USING ARG-LIST.

LINKAGE SECTION. 01 PARAM-LIST. 10 PART-ID PIC X(5). 10 SALES PIC 9(5). . . . PROCEDURE DIVISION USING PARAM-LIST.

Note: Number of parameters passed is different but length of passed value is same

CALL … BY CONTENT

• Passes Parameters, but the values do not get returned to the calling program.

CALL … BY CONTENT

Procedure Division. MOVE 4 to FLDA. MOVE 5 to FLDB.

CALL Linkage Type is procedure ‘SUBPGM1’ USING FLDA FLDB

CALL Linkage Type is program ‘SUBPGM2’ USING BY CONTENT FLDA FLDB

DISPLAY FLDA. DISPLAY FLDB.

Identification Division.PROGRAM-ID. SUBPGM1.Procedure Division using FLDX

FLDY. COMPUTE FLDX = FLDY * 5.

PROGRAM EXIT.

Identification Division.PROGRAM-ID. SUBPGM2.Procedure Division using FLDX

FLDY. COMPUTE FLDY = FLDX * 2. PROGRAM EXIT.

Calling Program Called Programs

CALL … BY CONTENT (OPM)

Procedure Division.

MOVE 4 to FLDA. MOVE 5 to FLDB.

CALL ‘SUBPGM1’ USING FLDA FLDB

CALL ‘SUBPGM2’ USING BY CONTENT FLDA FLDB

DISPLAY FLDA. DISPLAY FLDB.

Identification Division.PROGRAM-ID. SUBPGM1.

Procedure Division.…. COMPUTE FLDA = FLDB * 5. PROGRAM EXIT.

Identification Division.PROGRAM-ID. SUBPGM2.

Procedure Division.… COMPUTE FLDB = FLDA * 2. PROGRAM EXIT.

Calling Program Called Programs

CALL Statement (OPM)

Identification Division.

Procedure Division.

CALL ‘SUBPGM1’

CALL ‘SUBPGM2’

STOP RUN.

Identification Division.PROGRAM-ID. SUBPGM1.

Procedure Division.

PROGRAM EXIT.

Identification Division.PROGRAM-ID. SUBPGM2.

Procedure Division.

PROGRAM EXIT.

Calling Program Called Programs

CALL Statement (OPM)

Identification Division.

Procedure Division.

CALL ‘SUBPGM1’

CALL ‘SUBPGM2’

STOP RUN.

Identification Division.PROGRAM-ID. SUBPGM1.

Procedure Division.

STOP RUN.

Identification Division.PROGRAM-ID. SUBPGM2.

Procedure Division.

PROGRAM EXIT.

Calling Program Called Programs

44

Review of someBasics

Indexed Filesbasics

File Organizations

• Cobol supports 4 type of File Organizations• Sequential: Records stored in the same sequence

they arrived• Relative : Each record occupies a position in the

file relative to the beginning of the file (1th, 2nd, 3rd,…)

• Indexed : Records are stored in the file sorted by their record key

• TRANSACTION: used in interactive programming

The Indexed Organization• A record key is one of the record fields (e.g.

Student ID)• The field is permanently defined as the key of the

file at creation time• The value of a record key is the content of that

field for the file record being read, written, rewritten, or deleted

• In general, key values are unique (e.g. two students cannot have same ID’s)

The Record Key is a Field ACCT NUMBER CUSTOMER NAME CUST BIRTHDATE8811150552811 ROMEO LOPEZ 19,701,203 8811217643650 IAN J PHILLIPS 19,640,901 8811390394475 MARGARET TSUN 19,560,523 8811614491905 FERNE PHILLIPS 19,750,623 8811618790104 PENNY VITANZA 19,680,220 8811618909845 JAMES MALMSTROM 19,800,805 8812081630043 LESLIE R CHAN 19,600,725 8814280715195 ROBERT MILLER 19,501,101 8814288615652 ALLAN P MCLERIE 19,621,001 8815082500765 MARY E WILLIAMS 19,701,231 8816283476944 MICHELLE E ANDREOTI 19,720,206 8821292094514 ROBERT NOBLE 19,711,210

8830450892333 WILLIAM TELL 19,501,206

ACCT Number = Key Field

The Record Key Keep can access a Filesequentially in sorted key order either ascending or descending

ACCT NUMBER CUSTOMER NAME CUST BIRTHDATE8811150552811 ROMEO LOPEZ 19,701,203 8811217643650 IAN J PHILLIPS 19,640,901 8811390394475 MARGARET TSUN 19,560,523 8811614491905 FERNE PHILLIPS 19,750,623 8811618790104 PENNY VITANZA 19,680,220 8811618909845 JAMES MALMSTROM 19,800,805 8812081630043 LESLIE R CHAN 19,600,725 8814280715195 ROBERT MILLER 19,501,101 8814288615652 ALLAN P MCLERIE 19,621,001 8815082500765 MARY E WILLIAMS 19,701,231 8816283476944 MICHELLE E ANDREOTI 19,720,206 8821292094514 ROBERT NOBLE 19,711,210

8830450892333 WILLIAM TELL 19,501,206

ACCT Number = Key Field

The Record Key Allows for Random Accessto the records in an indexed file based on a key fields contents

ACCT NUMBER CUSTOMER NAME CUST BIRTHDATE8811150552811 ROMEO LOPEZ 19,701,203 8811217643650 IAN J PHILLIPS 19,640,901 8811390394475 MARGARET TSUN 19,560,523 8811614491905 FERNE PHILLIPS 19,750,623 8811618790104 PENNY VITANZA 19,680,220 8811618909845 JAMES MALMSTROM 19,800,805 8812081630043 LESLIE R CHAN 19,600,725 8814280715195 ROBERT MILLER 19,501,101 8814288615652 ALLAN P MCLERIE 19,621,001 8815082500765 MARY E WILLIAMS 19,701,231 8816283476944 MICHELLE E ANDREOTI 19,720,206 8821292094514 ROBERT NOBLE 19,711,210

8830450892333 WILLIAM TELL 19,501,206

Key Field = 8816283476944

Transaction File

CUSTMAINT Program

Customer Master File

Audit Report

Customer File Maintenance Program

System Diagram

Transaction File

CUSTMAINT Program

Customer Master File

Audit Report

Customer File Maintenance Program

System Diagram

PT004F

•Sequential

•Update/Deletion Requests

PRINT-FILE

PT003

•Indexed

•Record Key : Acct Number

•Customer Data

OPEN Files, Flag = Space

DO Header Line

START

END

READ FirstFirst Transaction Record

TRUE

DO Customer Record Update

DO Customer Record Deletion

DO Transaction Error Processing

CLOSE Files

ACTIVITY = ‘U’ ACTIVITY = ‘D’ OTHER

READ Next Next Transaction Record

DO

UNTIL FLAG = N

FLOWCHART FOR CUSTMAINT

Random Access: Accessing A Specific Record

• A record can be directly accessed based on its record key content

• Direct access can be used – to retrieve a record (e.g.READ), – to check if the record exists (e.g.READ),– to remove a record (e.g DELETE), – to update a record (e.g. REWRITE)– to add a new record and keep order (e.g. WRITE)

Random Access: Reading A Specific Record

• Invalid Key is true whenever the record cannot be retrieved

• The most important: Record Not Found

READ file-name INVALID KEY statementsEND-IF

Random Access: Rewriting A Specific Record

• Invalid Key is true whenever the record cannot be rewritten

• ie: key field contents changed

• A successful READ prior to REWRITE

REWRITE file-record-name INVALID KEY statementsEND-IF

Random Access: DELETING A Specific Record

• Invalid Key is true whenever the record cannot be deleted (key field contents changed)

• Value of key in key field before DELETE execution

DELETE file-name INVALID KEY statementsEND-IF

Random Access: Writing (ADDING) A Specific Record

• Invalid Key is on whenever the record cannot be added

• The most important: Duplicate (Existent) Key

WRITE file-record-name INVALID KEY statementsEND-IF

FILE STATUS FIELD

Used to catch errors created by the associated file i/o access (primarily disk).Automatically maintained / updated after each i/o activity by the system.! Including this into a program will avoid (a lot of) program aborts !

SELECT CUSTOMER-FILE ASSIGN TO DATABASE-PT003F ORGANIZATION IS INDEXED ACCESS MODE IS RANDOM RECORD KEY IS CUS-ACCTNUM FILE STATUS IS RETCODE.

WORKING-STORAGE SECTION.

77 RETCODE PIC XX. 88 SUCCESSFULL VALUE '00'. 88 DUPLICATE-KEY VALUE '22'.

FILE STATUS FIELD

PROCEDURE DIVISION. WRITE CUSTOMER-RECORD INVALID KEY MOVE ‘N’ TO ERROR-FLAG. IF SUCCESSFULL MOVE ' CUST. ADDED ' TO DET-CONDITION ELSE IF DUPLICATE-KEY MOVE ' DUPLICATE KEY' TO DET-CONDITION ELSE MOVE ' UNKNOWN ERROR' TO DET-CONDITION.

RETCODE is updated after the execution of the WRITE

RETCODE is updated after the execution of the WRITE

CONDITION NAMES: LEVEL 88WORKING-STORAGE SECTION.

77 HAIR-COLOR PIC X. 88 BLONDE VALUE ’1'. 88 BROWN VALUE '2'. 88 BLACK VALUE ‘3’.

DATA FIELD NAME CHANGES DEPENDING ON ITS CURRENT CONTENTS

PROCEDURE DIVISION.

IF BLONDE MOVE ‘BLONDE HAIR MODEL ' TO DET-HAIR-TYPE. IF BROWN MOVE ‘BROWN HAIR MODEL ' TO DET-HAIR-TYPE.

IF BLACK MOVE ‘BLACK HAIR MODEL ' TO DET-HAIR-TYPE.

62

COMPUTE Statement

• COMPUTE result-field = algebraic expression

• COMPUTE PAY = HOURS * RATE

• COMPUTE SALE = PRICE + (PRICE * 0.15)

• COMPUTE COUNTER = COUNTER + 1

• Result-field can be any numeric variable or an edited variable declared in the data division

• COBOL provides alternative verbs for each operator:

ADD SUBTRACT MULTIPLY DIVIDE

• COMPUTE requires less code and is more readable

63

COMPUTE Statement

• COMPUTE PAY = HOURS * RATE

MULTIPLY HOURS BY RATE GIVING PAY

• COMPUTE SALE = PRICE + (PRICE * 0.15)

MULTIPLY PRICE BY 0.15 GIVING TEMP

ADD TEMP, PRICE GIVING SALE

• COMPUTE COUNTER = COUNTER + 1

ADD 1 TO COUNTER

top related