More About COBOL Tables SFASU M. M. Pickard, PhD Copyright, 1996 © Dale Carnegie & Associates, Inc.
Introduction Beyond the basics: indexes versus subscripts the SET verb the SEARCH verb
Indexes versus Subscripts Definition (declaration). Modification. Reference. Lookup
Indexes versus Subscripts ----------Definition-------------- Appearance in ‘INDEXED BY’ clause 01 A-TABLE. 05 A-MESS PIC X(20) OCCURS 20 INDEXED BY A-INDEX. Definition as an integer data item 01 MISC. 05 XAMPL-SUB PIC 99.
Indexes versus Subscripts ----------modification------------ PERFORM…VARYING SET statement SEARCH statement PERFORM…VARYING arithmetic statements MOVE statement INITIALIZE statement
The SET statement Format 1: SET index-name-1 . . . TO index-name-2 or or identifier-1 identifier-2 or integer-1
The SET statement Format 2: SET index-name-1 UP BY identifier-1 or or DOWN BY integer-1
SEARCH verb Allows search of an indexed table Two search methods serial search binary search Table being searched MUST have a KEY clause and an INDEXED BY clause
SEARCH verb Serial search: SEARCH table-entry AT END executable-statement WHEN condition executable-statement(s) . . . [END-SEARCH]
SEARCH verb Binary search: SEARCH ALL table-entry AT END executable-statement WHEN equality condition executable-statement(s) [AND equality condition executable-statement(s)] . . . [END-SEARCH]
SEARCH verb Binary search SEARCH ALL (serial search doesn’t use ALL) Requires use of KEY clause on table in addition to INDEXED BY clause Elements of table must be in the specified order
Advantages of Indexed Tables Because indexes are stored in a format that is optimized for address calculation, table references for indexed tables are faster. An “automatic” binary search of an indexed table is possible.
Advantages of Subscripted Tables Subscripts are somewhat easier for beginners to understand.
Advantages of Subscripted Tables Debugging of code that uses subscripts is slightly easier. A subscript can be used with any table; an index can only be used with the table in which it is defined.
Summary - 1 Both subscripts and indexes can be used with tables having the INDEXED BY clause. Only subscripts may be used with non- indexed tables.
Summary - 2 Indexes have no PICTURE clause. Index values are kept in an internal format that is ideal for address computation. Indexes can not be adjusted with ordinary mathematical operations.
Summary - 3 Indexes are much faster Subscripts are slightly easier