Initializing tables.Use of tables with indexes.
Complete example.
indexexampleSearch.
Binary search.An example of a multi-dimensional table.
Sequential search
Search example.
[This chapter] [TOC] [Tutorial]
[This chapter] [TOC] [Tutorial]
[This chapter] [TOC] [Tutorial]
01 FIELD PIC X(10) OCCURS 10.
MOVE SPACE TO FIELD (3).or
MOVE FIELD (4) TO LINE.
[This chapter] [TOC] [Tutorial]
By using INITIALIZE, which refers to a higher level. All fields in the table are initialized in accordance with their data type. Normally, alphanumeric fields are set to SPACE and numeric to ZERO, but this is not mandatory.
By using VALUE, as in the main example below.3.
Or as in this example:
01 week. 03 day pic x(8) occurs 7 value from (1) "monday" "tuesday" "wednesday" "thursday" "friday" "saturday" "sunday".
This example is not applicable to all compilers.
[This chapter] [TOC] [Tutorial]
* Example of working with tables in COBOL. identification division. * ------------ program identication. program-id. table. author. kik. environment division. * ------------ program environment. configuration section. special-names. console is crt decimal-point is comma. data division. working-storage section. 77 in-1 pic 9999. 77 in-2 pic 9999. 77 tel pic 99. 77 lin pic 99. 01 result pic s9(6) occurs 5. 01 res-texts. 03 filler pic x(15) value "Addition". 03 filler pic x(15) value "Difference". 03 filler pic x(15) value "Quotient". 03 filler pic x(15) value "Remainder". 03 filler pic x(15) value "Product". 01 res-text redefines res-texts pic x(15) occurs 5. 01 Out-field. 03 text pic x(15). 03 tal1 pic ---.--9. procedure division. * ------------ executable instructions. main section. display space. display "Enter 1st figure :" line 5 position 10. accept in-1 line 5 position 27. display "Enter 2nd figure :" line 6 position 10. accept in-2 line 6 position 27. add in-1, in-2 giving result (1). subtract in-2 from in-1 giving result (2). divide in-1 into in-2 giving result (3) remainder result (4). multiply in-1 by in-2 giving result (5). perform varying tel from 1 by 1 until tel > 5 move space to Out-field move res-text (tel) to text move result (tel) to tal1 add tel, 7 giving lin display Out-field line lin position 1 end-perform. exit program.
[This chapter] [TOC] [Tutorial]
01 FIELD PIC X(10) OCCURS 10 INDEXED BY IDX.
SET IDX TO 3. MOVE SPACE TO FIELD (IDX) .
SET IDX UP BY 1. MOVE FIELD (IDX) TO LINE.
[This chapter] [TOC] [Tutorial]
* Example of working with tables in COBOL. identification division. * ------------ program indentifiaction. program-id. table. author. kik. environment division. * ------------ program environment. configuration section. special-names. console is crt decimal-point is comma. data division. working-storage section. 77 in-1 pic 9999. 77 in-2 pic 9999. 77 lin pic 99. 01 result pic s9(6) occurs 5 indexed by tel1. 01 res-texts. 03 filler pic x(15) value "Addition". 03 filler pic x(15) value "Difference". 03 filler pic x(15) value "Quotient". 03 filler pic x(15) value "Remainder". 03 filler pic x(15) value "Product". 01 res-text redefines res-texts pic x(15) occurs 5 indexed by tel2. 01 Out-field. 03 text pic x(15). 03 tal1 pic ---.--9. procedure division. * ------------ executable instructions. main section. display space. display "Enter 1st figure :" line 5 position 10. accept in-1 line 5 position 27. display "Enter 2nd figure :" line 6 position 10. accept in-2 line 6 position 27. add in-1, in-2 giving result (1). subtract in-2 from in-1 giving result (2). divide in-1 into in-2 giving result (3) remainder result (4). multiply in-1 by in-2 giving result (5). perform varying tel1 from 1 by 1 until tel1 > 5 move space to out-field set tel2 to tel1 move res-text (tel2) to text move result (tel1) to tal1 set lin to tel1 add lin, 7 giving lin display Out-field line lin position 1 end-perform. exit program.
[This chapter] [TOC] [Tutorial]
[This chapter] [TOC] [Tutorial]
[This chapter] [TOC] [Tutorial]
[This chapter] [TOC] [Tutorial]
* Example of how to use SEARCH in a table in COBOL. identification division. * ------------ program identification. program-id. searchtab. author. kik. environment division. * ------------ program environment. configuration section. special-names. console is crt decimal-point is comma. data division. working-storage section. 77 teller pic 99. 77 lin pic 99. 01 eot-code pic 9 value 0. 88 eot value 1. 88 not-eot value 0. 01 tabel occurs 50 ascending key is key indexed by idx. 03 key pic 999. 03 text pic x(10). procedure division. * ------------ executable instructions. main section. display space. perform init-table varying idx from 1 by 1 until idx > 50. * Binary search : search all table when key (idx) is = 10 next sentence. set teller to idx. display "Key found in idx =" line 10 position 1. display teller line 10 position 24. * Sequential search from index = 10 and thereafter. move 15 to lin. perform sequ-search. * Sequential search from index = 1 and thereafter. set idx to 1. move 17 to lin. perform sequ-search. stop run. init-table. set key (idx) to idx. move space to text (idx). sequ-search. set not-eot to true. search table varying idx at end set eot to true when key (idx) = 5 next sentence. if not-eot then set teller to idx display "Key found in idx =" line lin position 1 display teller line lin position 24 else display "Key not found" line lin position 1.
[This chapter] [TOC] [Tutorial]
01 Table occurs 10. 03 field-1 pic xxx. 03 sub-table pic 9(5) occurs 5. 03 field-2 pic 999.It is then possible to refer to an entire record:
table (idx)
field-1 (idx)
sub-table (idx1, idx2)
[This chapter] [TOC] [Tutorial]
01 source. 03 table occurs 100. (The table used in the program) 05 s-1 ........ 05 s-2 ........ 01 x redefines source. (for purposes of initialisation only) 03 dummy. 05 filler ...... (defined as s-1) 05 filler ...... (defined as s-2) 03 target. 05 filler .... occurs 99. (defined as s-1) 05 filler .... occurs 99. (defined as s-2) . . . move ... to s-1. (First entry is initialised) move ... to s-2. move source to target. (First entry is "spread")
[This chapter] [TOC] [Tutorial]
- COBOL's facilities for handling sequential files
- Selections in COBOL
- Calculations
- Numeric editing
- Output data to the terminal.
- table handling
- subprograms
- Read the file described below
- Provide a total for each value in the field TR-CODE
- Present the result on the screen.
Customer number, 4 characters, value set: 0-9999.
Transaction code, 2 characters, value set: 1, 2, 3, 4, 99.
Movement, 6 characters + 2 decimals (no decimal point).
Balance, 6 characters + 2 decimals (no decimal point).
The file is not to be sorted.Here is an extract of the file:
The "Movement" field should only apply to transaction codes 1-4.
The "Balance" field should only relate to transaction code 99.
4711010001230000000000 7100020011002500000000 5500990000000000043200