Skip to content

Commit c85e072

Browse files
committed
Add example on index displacement calculation and fix one more grammatical issue
Signed-off-by: Hartanto Ario Widjaya <[email protected]>
1 parent 3e931b4 commit c85e072

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

COBOL Programming Course #1 - Getting Started/COBOL Programming Course #1 - Getting Started.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1944,7 +1944,7 @@ We can also nest multiple OCCURS elements to create a table of additional dimens
19441944
15 ASSIGMMENT-WEIGHTAGE PIC 9(03).
19451945
```
19461946

1947-
Here, we are defining a degree program which has 10 courses and each course will have 8 assignments. What if we don't know how many times will a table element will occur? To solve that, we can use variable-length table, using the OCCURS DEPENDING ON (ODO) clause which we will be going into more details on a later section.
1947+
Here, we are defining a degree program which has 10 courses and each course will have 8 assignments. What if we don't know how many times a table element will occur? To solve that, we can use variable-length table, using the OCCURS DEPENDING ON (ODO) clause which we will be going into more details on a later section.
19481948

19491949
## Referring to an item in a table
19501950

@@ -2000,6 +2000,30 @@ The integer there represents the number of occurences. So it will be converted t
20002000

20012001
Since we are comparing physical displacements, we cannot use index data items as subscripts or indexes. We can only directly use it in SEARCH and SET statements or in comparisons with indexes.
20022002

2003+
The following example shows how to calculate displacements to elements that are referenced with indexes.
2004+
2005+
Consider the following two dimensional table, TABLE-2D:
2006+
2007+
```
2008+
01 TABLE-2D.
2009+
05 TABLE-ROW OCCURS 2 TIMES INDEXED BY INX-A.
2010+
10 TABLE-COL OCCURS 5 TIMES INDEXED BY INX-B PIC X(4).
2011+
```
2012+
2013+
Suppose we code the following index:
2014+
2015+
```
2016+
TABLE-COL (INX-A + 2, INXB - 1)
2017+
```
2018+
2019+
This will cause the computation of the displacement to the TABLE-COL element:
2020+
2021+
```
2022+
(contents of INX-A) + (20 * 2) + (contents of INX-B) - (4 * 1)
2023+
```
2024+
2025+
The calculation is based on the length of the elements. Each occurence of TABLE-ROW is 20 bytes in length (5 * 4) and each occurence of TABLE-COL is 4 bytes in length.
2026+
20032027
## Loading a table with data
20042028

20052029
There are many ways we can load a table. The first one involves loading the table dynamically, from a screen, file or database. We can also use the REDEFINES clause on hard-coded field values along with an OCCURS clause. The third way is using the INITIALIZE statement, and lastly, we can also use the VALUE clause when defining the table.

0 commit comments

Comments
 (0)