0% found this document useful (0 votes)
58 views17 pages

Array Processing and Table Handling

The document discusses using arrays and tables to efficiently process series of input fields in COBOL. It describes defining arrays using the OCCURS clause to represent multiple fields of the same format. Elements within arrays can be accessed using subscripts. Tables store fields similarly to arrays but are used for table lookups to find specific entries. Indexes are used with the SEARCH statement to access table elements, where the index is defined along with the table.

Uploaded by

Lokesh Sivakumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views17 pages

Array Processing and Table Handling

The document discusses using arrays and tables to efficiently process series of input fields in COBOL. It describes defining arrays using the OCCURS clause to represent multiple fields of the same format. Elements within arrays can be accessed using subscripts. Tables store fields similarly to arrays but are used for table lookups to find specific entries. Indexes are used with the SEARCH statement to access table elements, where the index is defined along with the table.

Uploaded by

Lokesh Sivakumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 17

Chapter 12

Array Processing and Table


Handling
Defining Series of Input Fields
• Coding record with 24 independent hourly
fields is cumbersome
01 Temp-Rec.
05 One-AM Pic S9(3).
05 Two-AM Pic S9(3).
24 entries
... … …
05 Midnight Pic S9(3).
Defining Series of Input Fields
• To obtain average temperature requires
summing 24 fields

Compute Avg-Temp = (One-AM +


Two-AM + … + Midnight) / 24
OCCURS Clause
• Defining series of input or output fields,
each with same format
• Defining series of totals to which amounts
added
• Defining a table to be accessed using
contents of input field to 'look up' required
data
Defining Fields with OCCURS
• Since all 24 fields have same PICTURE
– Can define entire 72-position area as array
– Array divided into 24 three-position fields,
called elements

01 Temp-Rec.
05 Temperature Occurs 24 Times
Pic S9(3).
Accessing Elements in Array
• Identifier Temperature is array name
• Use array name along with a subscript to
access fields or elements within array
• Subscript indicates which of the 24
elements to access
Statement Output
Display Temperature (2) 2 AM value
Display Temperature (23) 11 PM value
Subscripts
• Using a data-name as a subscript enables
its contents to be varied
• Each time the value of a data-name
changes, Temperature (Sub) refers to a
different element in the array
• Then a single routine can be used to
process all elements in array
Valid Subscripts
• Valid values are 1 to number of elements
in array
• For array Temperature valid subscripts are
1 to 24
• Invalid use of subscripts
– Display Temperature (0)
– Display Temperature (25)
– Display Temperature(8)
– Display Temperature ( 2 )
Tables
• Table is list of stored fields
• Stored same way as array but used for
different purpose
• Used with table look-ups, a procedure to
find specific entry in a table
• E.g., A record with two fields, zip code and
sales taxes. Read-in records, storing each
zip code-tax rate combination in a table.
Table Look-Up
Table entries in WORKING-STORAGE
Table Argument Table Function
Zip Code Sales Tax Rate
Input Record 00123 ^060

12344 00456 ^075

Zip Code 10111 ^065


Rate for Zip
(search 12344 ^080 of 12344

argument) 25033 ^070

… ...
Table Look-up with PERFORM
Move 0 To WS-Sales-Tax
Perform Varying X1 From 1 By 1
Until X1 > 1000
If Zip-In = WS-ZipCode (X1)
Compute WS-Sales-Tax Rounded =
WS-Tax-Rate (X1) *
Unit-Price-In * Qty-In
End-If
End-Perform
SEARCH Statement
Format
SEARCH identifier-1
[AT END imperative-statement-1]
WHEN condition-1
imperative-statement-2 ...
CONTINUE
[END-SEARCH]
• Use in place of PERFORM to search table
SEARCH Statement Example
Set X1 To 1
Search Table-Entries
At End Move 0 To WS-Sales-Tax
When Zip-In = WS-ZipCode (X1)
Compute WS-Sales-Tax Rounded =
WS-Tax-Rate (X1) *
Unit-Price-In * Qty-In
End-Search
INDEXED BY clause
• Special field called index must be used
with SEARCH
• Similar to subscript but defined along with
table as part of OCCURS
05 Table-Entries Occurs 1000 Times
Indexed By X1. X1 defined as index

• Compiler automatically supplies


appropriate PICTURE clause for index
Subscripts vs Indexes
• Subscript
– Represents occurrence of array or table
element
• Index
– Represents value used internally to actually
access table entry (a displacement from first
address in array or table)
Subscripts vs Indexes
• Subscript
– Defined in separate WORKING-STORAGE
entry
– May be used any where field with its
PICTURE is allowed
• Index
– Defined along with OCCURS
– May be used only with table for which it was
defined
Subscripts vs Indexes
• Subscript
– Value may be changed using PERFORM …
VARYING
– Also by MOVE or arithmetic statements
• Index
– Value may be changed using PERFORM …
VARYING
– SET only other statement to modify index

You might also like