1 / 23

COBOL

COBOL . Arrays. Paragraphs. A Paragraph Name is a programmer defined name for a block of code. Must use the standard rules for programmer defined names (A-Z, 0-9, -). Must be terminated with a full-stop.

brooke
Download Presentation

COBOL

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COBOL Arrays

  2. Paragraphs • A Paragraph Name is a programmer defined name for a block of code. • Must use the standard rules for programmer defined names (A-Z, 0-9, -). • Must be terminated with a full-stop. • Any number of statements and sentences may be included in a paragraph but the last one must be terminated with a full-stop. • The code for a Displaced Paragraph is after the STOP RUN but is called from within the main program.

  3. PERFORM • PERFORM is COBOL’s only iteration construct, and it comes in several flavours. • PERFORM … TIMES executes a block of code or a Paragraph a certain number of times. E.g. Perform 3 TIMES • PERFORM … UNTIL executes a block of code or a Paragraph until a certain condition is satisfied. • The WITH TEST BEFORE clause means that the UNTIL condition is tested before the loop body has been entered / executed. (DEFAULT) • The WITH TEST AFTER clause means that the UNTIL condition is tested after the loop body been entered / executed.

  4. PERFORM • PERFORM Print-Prime-Numbers VARYING Prime-Num FROM1 BY 1 UNTIL Prime-Num > 9999 • Out-of-Line • Performing a separate Paragraph • In-Line • Performing a chunk of code contained within the PERFORM .... END-PERFORM

  5. Single Dimensional (1-D Arrays) • Single row or column of pigeon holes or boxes used to hold pieces of information. • They provide storage of data and allow retrieval of data when required. •  To access a particular array location, you use an index value to say which pigeon hole or box you want to access.

  6. 1-D Arrays LetterBox • To deliver a letter to Apt. 15 need to walk along the row of letter boxes until they get to box number 15 before they can insert the mail into right letterbox. • To check mail, then need to walk along the row of letter boxes until get to box number 15 before can open it and check my mail. • Row of letter boxes forms a simple 1-D Array • Letter box number provides an index which can be used to locate and access data in a particular letter box. • The location (index) of my letter box is 15. • Normally each pigeon hole (box) in the Array, is called a cell.

  7. Defining arrays • Arrays (also known as Tables) provide a way of holding / manipulating multiple values for a field or a group of fields. • Providing storage for multiple pieces of the same information. • The various pieces of data are referenced by using a numeric index, so that particular entries in the array can be set, accessed, manipulated, or altered. • Array fields can be used exactly as you would any other data field in COBOL.

  8. Defining Arrays • An array is a group item, with each of its elements belonging to it • Each element has the same picture and the element occurs a number of times • Subscripting always starts at 1. • Arrays are fixed length. 01 Months-of-year. What picture? 03 Month-name pic aaa occurs 12 times. The items are month-name(1) to month-name(12)

  9. Defining Arrays • 01 <name of structure. • 02 <name of cells> occurs <no of cells> • Indexed By <name of index>

  10. Defining the index • Subscripting can be done by an independent data item or • A subscripting item can be declared with the array:- 01 Months-of-year. 03 Month-name pic aaa occurs 12 times indexed by MONTH-NO. • In this case, month-no is defined as only having a value between 1 and 12. • Month-no can be assigned a value using the SET command: Set Month-no to 1.

  11. Auto-increment loop • Equivalent to “For a:= 1 to 10 do” • The counter is given a start value, an end condition and an increment value: Perform <paragraph> varying Month-no from 1 by 1 until Month-no > 12 • Start value is 1, increment is 1, condition is Month-no > 12 • The paragraph is performed repeatedly • The condition can be tested before or after the paragraph (See “with test after” in last topic)

  12. Examples – Month Names • Often in Cobol, it is useful to convert the month number (such as 6) to the month name (such as "Jun"). • Can either do a whole series of IF tests or a 12 line EVALUATE statement. • Or we could define an array of month names and use the particular month number as an index to get the required month name from the array.

  13. Examples – Month Names • Need to define some array table fields in the WORKING-STORAGE SECTION. • Need to define a field containing the names of all 12 months • Need to create another field that references or REDEFINES this data into an array structure. • Note that we can only use the OCCURS clause on data in levels 02-49.

  14. Example – Month Names 01 W-Month-Data. 03 FILLER PIC X(36) VALUE "JanFebMarAprMayJunJulAugSepOctNovDec". 01 W-Month-Array-Table REDEFINES W-Month-Data. 03 W-Month-Array OCCURS 12 TIMES PIC X(3).

  15. 2-Dimensional Arrays • Grid or Matrix • i.e. rows and columns of boxes. Rainfall Data

  16. 2-Dimensional arrays • Each array element is a group, containing another array. 01 Box-of-chocolates. 05 Layer-of-chocolates occurs 4 times indexed by layer-no. 10 Chocolate occurs 20 times pic 99 indexed by choc-no. • An individual chocolate can be referenced as:- chocolate (layer-no, choc-no)

  17. Parallel 1-D arrays. • An array element can be a group, with individual group members being accessed separately:- 01 LAB-class. What pictures? 03 LAB-student occurs 20 times indexed by student-no. 05 Student-name pic x(20). 05 Student-machine-no pic AA99. • Student-name (1) to Student-name (20) • Student-machine-no (1) to Student-machine-no(20) • Which are the contents of • Lab-Student(1) to Lab-Student (20)

  18. Parallel 1-D Example • Suppose we have the following : • Job Codes PIC X(4) • Job Descriptions PIC X(20) • There are 1000 Job Codes and associated descriptions • How to we represent the data ? 01 W-Job-Array. 10 Job-Code-Array PIC X(4) OCCURS1000 TIMES. 10 Job-Desc-Array PIC X(20)OCCURS1000 TIMES. OR 01 W-Job-Array. 05 W-JobData OCCURS1000 TIMES. 10 Job-Code-Array PIC X(4). 10 Job-Desc-Array PIC X(20).

  19. Parallel 1-D Example Working Storage. 01 W-Count Pic 9(3). 01 W-Num-Rows Pic 9(3) value 1000. 01 W-Match-Found Pic x. MOVE "F" TO W-Match-Found PERFORM VARYING W-Count FROM 1 BY 1 UNTIL (W-Count > W-Num-Rows) OR (W-Match-Found = "T") IF Job-Code-Array (W-Count) = "X123" DISPLAY “Found “ Job-Code-Array (W-Count) “ “ Job-Desc-Array (W-Count) MOVE "T" TO W-Match-Found END-IF END-PERFORM

  20. 4 8 1 6 5 4 7 9 2 Putting values into arrays. • The value clause cannot be used with occurs • To put initial values into an array, the area needs to be redefined. • Values can be put into the redefined area • The values appear in the array 01 defined-area. 02 matrix. 03 matrix-row occurs 3 indexed by I-row. 04 matrix-element pic 9 occurs 3 indexed by I-col. 02 value-set redefines matrix pic x(9) value “481654792”.

  21. Arrays Identification Division. Program-Id. Array-handling. *Introducing 2 1-D arrays; one pre-defined *and the other to be filled. Environment Division. Configuration Section. Special-names. Currency sign is ӣ".

  22. Data Division. Data Division. Working-Storage Section. 01 Months-of-year. 03 Month-name pic aaa occurs 12 times indexed by MONTH-NO. 01 Month-names pic x(36) value "JanFebMarAprMayJunJulAugSepOctNovDec". 01 Months-expenditure. 03 Month-spending pic s9(9)v99 occurs 12 times indexed by Mon-no 01 Tot-exp pic s9(11)v99 value 0. 01 Display-y-exp pic £££,£££,£££,£££.99CR.

  23. Procedure Division Procedure Division. Move Month-names to Months-of-year. Perform varying Month-no from 1 by 1 until Month-no > 12 Display Month-name (month-no), ":" with no advancing Set Mon-no to Month-no Accept Month-spending (Mon-no) Add Month-spending (Mon-no) to Tot-exp End-Perform Move Tot-exp to Display-y-exp Display " ". Display "Total yearly expenditure is “ Display-y-exp. Stop run.

More Related