63
Multi-Dimensional Arrays

Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

Embed Size (px)

Citation preview

Page 1: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

Multi-Dimensional Arrays

Page 2: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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

Page 3: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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).

Page 4: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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).

Page 5: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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).

Page 6: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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)

Page 7: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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.

Page 8: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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.

Page 9: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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

Page 10: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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.

Page 11: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

Structured Programming

Series of Independent Modules executed from a main module.

Page 12: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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

Page 13: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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

Page 14: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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}

Page 15: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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

Page 16: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

Calling programsand

passing parameters(values)

Page 17: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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

Page 18: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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)

Page 19: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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

Page 20: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

20

OPM Disadvantages

• Many calls leads to performance problems

• Compatibility problems between two languages.

Page 21: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

21

Integrated Language Environment

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

Page 22: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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

Page 23: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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’

Page 24: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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

Page 25: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

Combining Modules

Example

MODA *MODULE calls

MODB *MODULE

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

Page 26: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

26

ILE Benefits

• Resolves language incompatibility issues

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

time

Page 27: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

27

ILE Disadvantages

• Uses more disk space.

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

• Version control. (none)

Page 28: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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!

Page 29: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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)

Page 30: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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

Page 31: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

31

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

*MODULE

*MODULE

*MODULE

MULTI MODULE MODULE PEP!

*module

*module*module

Page 32: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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

Page 33: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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.

Page 34: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

Calling ProgramsProgram being called

• Must end with EXIT PROGRAM instead of STOP RUN.

• Because … STOP RUN terminates the entire process / application

Page 35: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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.

Page 36: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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

Page 37: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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

Page 38: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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

Page 39: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

CALL … BY CONTENT

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

Page 40: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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

Page 41: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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

Page 42: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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

Page 43: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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

Page 44: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

44

Review of someBasics

Page 45: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

Indexed Filesbasics

Page 46: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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

Page 47: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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)

Page 48: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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

Page 49: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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

Page 50: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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

Page 51: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

Transaction File

CUSTMAINT Program

Customer Master File

Audit Report

Customer File Maintenance Program

System Diagram

Page 52: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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

Page 53: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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

Page 54: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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)

Page 55: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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

Page 56: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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

Page 57: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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

Page 58: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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

Page 59: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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'.

Page 60: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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

Page 61: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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.

Page 62: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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

Page 63: Multi-Dimensional Arrays. Time Table PeriodMondayTuesdayWedThursFri 5BAC344AIBC233BStudent Help 6BAC344AIBC233BStudent Help 7IBC233AStudent Help IBC233AB

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