|
1
|
- Jon Sayles, IBM Software Group, Rational EcoSystems Team
|
|
2
|
- © Copyright IBM Corporation 2007,2008.
All rights reserved.
- The information contained in these materials is provided for
informational purposes only, and is provided AS IS without warranty of
any kind, express or implied.
IBM shall not be responsible for any damages arising out of the
use of, or otherwise related to, these materials. Nothing contained in these materials
is intended to, nor shall have the effect of, creating any warranties
or representations from IBM or its suppliers or licensors, or altering
the terms and conditions of the applicable license agreement governing the use of IBM software.
References in these materials to IBM products, programs, or services do
not imply that they will be available in all countries in which IBM
operates.
- This information is based on current IBM product plans and strategy,
which are subject to change by IBM without notice. Product release
dates and/or capabilities referenced in these materials may change at
any time at IBMs sole discretion based on market opportunities or
other factors, and are not intended to be a commitment to future
product or feature availability in any way.
- IBM, the IBM logo, the on-demand business logo, Rational, the Rational
logo, and other IBM Rational products and services are trademarks or
registered trademarks of the International Business Machines
Corporation, in the United States, other countries or both. Other
company, product, or service names may be trademarks or service marks
of others.
|
|
3
|
- Thanks to the following individuals, for assisting with this course:
|
|
4
|
- Course Name: COBOL Foundation
Training - with RDz
- Course Description: Learn the
COBOL language, RDz and learn z/OS terms, concepts and development
skills in this course.
- Pre-requisites: Some experience in a 3rd or 4th Generation Language is
expected. SQL is also
recommended.
- Course Length: 10 days
- Topics (Agenda)
- Getting Started - installing and configuring RDz - and the course materials, and using Eclipse to edit
COBOL
- COBOL General Language Rules
- Basic COBOL Statements
- Additional record and table handling
- Debugging Programs - Note: Deep dive on using RDz for common COBOL
programming errors (001, 0C4, 0C7, infinite loops, fall-thru, etc.)
- Input/Output and Report Writing Patterns
- COBOL Subprograms and the Linkage Section
- Structured Programming Concepts, professional COBOL development
practices and Coding Patterns
- Advanced Character Manipulation, COBOL Intrinsic Functions, Date and
Time coding patterns, and Language Environment calls
- OS/390 Concepts and JCL - Compile/Link & Run Procs on the
mainframe
- Indexed file Coding Patterns
- Sort/Merge, Sequential File Match/Merge and Master File Update Coding
Patterns
- Accessing DB2 Data and DB2 Stored Procedures
- COBOL in the Real World:
- CICS - lecture only
- IMS (DL/I and TM) - ditto
- Batch processing - ditto
- Java calling COBOL
- COBOL and XML Statements
- SOA and COBOL - creating and calling Web Services
- Web 2.0 using Rich UI
|
|
5
|
- Audience
- This course is designed for application developers who have programmed
in some language before, and who wish to learn COBOL.
- Prerequisites
- This course assumes that the student has the basic knowledge of IS
technologies, data processing, software and have programmed for at
least two or more years in a language such as: Java, VB, RPG, PL/1,
Pascal, or some 4th Generation Language or tool.
- Knowledge of SQL (Structured Query Language) for database access is
assumed as well.
- Basic PC and mouse-driven development skills is also assumed.
- Finally, it is assumed that you have been following along in this
course, and have successfully completed the learning modules in
sequence.
- Or have the equivalent COBOL background obtained through some other
form of COBOL study or on-the-job work.
|
|
6
|
|
|
7
|
- COBOL records are hierarchical structures that allow you to organize
("model") sophisticated real-world complex data
- Records have a 01 level number
definition
- Coded beginning in the "A" margin
- Typically with no PIC clause
- And records continue with one-to-many elementary items some of which
may be "group" data items (with no PIC clause)
- Here's an example shown in COBOL and graphically (with data)
|
|
8
|
- In many of the COBOL applications you will work in, you will encounter
situations where fields in records are REDEFINED (the actual COBOL
keyword is REDEFINES) with different PIC clauses.
- REDEFINES means that the same area of storage in the DATA DIVISION can
be referenced in two or different ways.
- Here's an example:
- Assume an HR application stores employee mailing addresses
- As up-to three lines of street-address, or a P.O. Box
- Two records from the employee external file in succession:
|
|
9
|
- Language Rules How to declare or REDEFINE a field:
- You redefine a field:
- At the same level number
- The level numbers can not be: 88, 66, 01 (in the FILE SECTION) but
you can REDEFINE an 01 record in WORKING-STORAGE
- Immediately following the field with no higher level numbers between
(higher in the COBOL hierarchy)
- The data description entry for other-data-item cannot contain an OCCURS
clause
- The redefining field should (does not have to, but should) have the same
number of characters as the Redefined field
- You can have multiple redefinitions of the same field's character
positions.
- However, they must all refer to other-data-item, the data-name that
originally defined the area.
- The redefining entries cannot contain VALUE clauses except in
condition-name entries.
- How to tell which field to reference:
- (The redefining or the redefined)
- Typically your record will have a flag or some sort of indicator
variable that distinguishes the record types.
- Usually this is one-byte "flag" PIC X field that carries a value which
describes the data
that follows
- See the example on the right θ
|
|
10
|
- In the WORKING-STORAGE SECTION (not in the FILE SECTION) you may
redefine at the 01-record level.
- This was common practice in older systems that were memory or
storage-constrained.
- Typically you might have (at least) 3 different records read:
- Header-Rec: With Job run-date information, Company Name, etc.
- Detail-Rec: With the base information to be processed
- Note that within the detail record, you might very well find
additional REDEFINE fields
- Trailer-Rec: With more balancing total amounts, etc
- Here's an example
- H01/01/2008|Acme Motor Corporation|
- D
|
|
|
|
|
|
|
|
- D
|
|
|
|
|
|
|
|
-
- T0908432|984320432|
- And to process:
- READ IN-FILE INTO HEADER-REC.
-
- IF Rec-Type = "H"
-
Perform Header Record
Processing
- ELSE IF Rec-Type = "D"
-
Perform Detail Record
Processing
- Else
Perform Trailer Record
Processing
|
|
11
|
- From the course workshop documents, do the following labs:
- Data Representation and assignment (MOVE statement) Lab
- Open ended workshop
|
|
12
|
|
|
13
|
- By the end of this unit you should be able to:
- Describe the COBOL OCCURS clause and code a syntactically correct
example
- Describe the differences between a subscript and table index
- Create COBOL one-dimension table, by redefining values in
Working-Storage
- Initialize and load data in a COBOL table
- Refer to specific table row/field table occurrences in the Procedure
Division
|
|
14
|
- Like most programming languages, COBOL can group variables of the same
type in internal program tables.
- COBOL supports the following kinds of table structures:
- Single COBOL field tables
- Fixed-length Structure field tables the most common format
- Variable-length tables where the number of table occurrences is not
known until run-time
- From this example:
- Sample-Table-One is a group data item
- Table-Column is a group data item that is repeated three times
- You can conceptualize fields with an OCCURS clause as:
- Blocks of contiguous storage
if you visualize the data from left to right
- Rows if you're visualizing the data in storage vertically
- Table-Item-1 and Table-Item-2 are elementary data fields that are
repeated three times, as they are child variables of Table-Column
|
|
15
|
- General information on COBOL tables:
- Are one(1)-based in subscripts - not zero-based
- The value within the parenthesis must be: > 0 and <= the table's OCCURS
limit
- Attempts to access COBOL table data beyond its internal size results
in a run-time error: S0C4 index
or array out of bounds
- Table rows (or columns) are known as "occurrences"
- Tables an have multiple dimensions
- Up to seven levels of table OCCURS nested within OCCURS in modern
COBOL compilers
- Older COBOL only supported three levels
- (Op-Ed) In standard business programming, tables > three levels
deep are rare
- You can use special index datatype values to reference table row data,
or you can use subscripts which are numeric integer variables and
values
|
|
16
|
- There are generally two reasons for tables in any language, including
COBOL:
- To simplify repetitive processing patterns and organize related
repetitive data
- To handle design issues that require dynamic, run-time-sized
structures
- Example: You want to process quarterly historical data going back n
years, your options are:
|
|
17
|
- There are four basic categories of things to learn about COBOL tables:
- Define COBOL tables
- Most of the time in WORKING-STORAGE
- Specific syntax examples follow
- Load COBOL tables
- From files or databases
- Programmatically from within the PROCEDURE DIVISION
- Initialize and re-initialize COBOL tables
- Process COBOL tables including:
- Using the values procedurally in business logic computations and data
manipulation
- Searching the tables for matching values in the PROCEDURE DIVISION
upcoming section
|
|
18
|
- To define a table:
- Give the table a group name,
then define a subordinate item (the table element) to be
repeated n times.
- 01 table-name.
- 05 element-name OCCURS n TIMES. . . . (subordinate items of the
table element)
- In the example above
- table-name is the name of an alphanumeric group item
- There would (typically) be one-to-many subordinate items (fields at
level 10 or higher) within a structure
- The table element definition (which includes the OCCURS clause) is
subordinate to the group item that contains the table.
- OCCURS
- Can appear on level: 02 ΰ 49
- Cannot appear on levels: 01 or 77
- Optional clause: INDEXED BY
<idxName>.
- If you declare a COBOL table and include the INDEXED BY clause, the
COBOL compiler will create an internal subscript you will use to
reference table occurrences
- Using INDEXED BY can be substantially more efficient than using
subscripts particularly for large tables (> 1,000 rows)
|
|
19
|
|
|
20
|
- You can load a table:
- Programmatically (from screen, file or database values) next topic
- REDEFINE constant field values with an OCCURS (previous slides)
- Using Initialize see example below
- Using the VALUE clause on elementary field definitions.
- Initialize examples, to move the value 3 into each of the elementary
numeric data items in a table called TABLE-ONE, shown below, you can
code the following statement:
- INITIALIZE TABLE-ONE REPLACING NUMERIC DATA BY 3.
- To move the character 'X' into each of the elementary alphanumeric data
items in TABLE-ONE, you
can code the following statement:
- INITIALIZE TABLE-ONE REPLACING ALPHANUMERIC DATA BY "X".
- When you use the INITIALIZE statement to initialize a table, the table
is processed as a group item (that is, with group semantics); elementary
data items within the group are recognized and processed.
- You can use the REPLACING phrase of the INITIALIZE statement similarly
to initialize all of the elementary fields with these datatypes in a
table:
- ALPHABETIC, DBCS, ALPHANUMERIC-EDITED, NATIONAL-EDITED, NUMERIC-EDITED
- The INITIALIZE statement cannot assign values to a variable-length table
- A table that was defined using OCCURS DEPENDING ON
|
|
21
|
- Consider the following table declaration:
- 01 TABLE-ONE.
- 02 Trans-out Occurs 20.
- 05 Trans-code PIC X Value "R".
- 05 Part-number PIC XX Value "13".
- 05 Trans-qty PIC 99 Value 10.
- 05 Price-fields.
- 10 Unit-price PIC 99V Value 50.
- 10 Discount PIC 99V Value 25.
- 10 Sales-Price PIC 999 Value 375
- . . . .
- PROCEDURE DIVISION.
- . . . .
- INITIALIZE TABLE-ONE
- Replacing Numeric Data By 3 Alphanumeric Data By "X".
- The table below shows the content that each of the twenty 12-byte
elements Trans-out(n) has before execution and after execution of the INITIALIZE
statement shown above:
|
|
22
|
- If the initial values of your table are different with each execution of
your program, you can define the table without initial values.
- You can read the changed values into the table before your program
refers to the table at the beginning of the PROCEDURE DIVISION
- Use the PERFORM statement and either subscripting or indexing.
- When reading data to load your table, test to make sure that the data
does not exceed the space allocated for the table (defined by OCCURS)
- Use a named value (rather than a literal) for the item count.
- If you end up making the table bigger, you need to change only one
value, instead of all references to a literal
|
|
23
|
- The lowest possible subscript or index value is 1, which references the
first occurrence of a table element.
- In a one-dimensional table, the subscript/index corresponds to the row
number.
- You can use a literal or a data-name as a subscript.
- If a data item that has a literal subscript is of fixed length, the
compiler resolves the location of the data item.
- When you use a data-name as a variable subscript, you must describe the
data-name as an elementary numeric integer.
- If you have defined the COBOL table with INDEXED BY <indexName> -
you will use the indexName as the subscript
- You can increment or decrement an index, or literal or variable
subscript by a specified integer amount.
For example:
- TABLE-COLUMN (SUB1 - 1, SUB2 + 3)
- You can change part of a table element rather than the whole element. To
do so, refer to the character position and length of the substring to be
changed. For example:
- 01 ANY-TABLE.
- 05 TABLE-ELEMENT PIC X(10) OCCURS 3 TIMES VALUE "ABCDEFGHIJ"
- . . . .
- MOVE "??" TO TABLE-ELEMENT (1) (3 : 2).
- The MOVE statement in the example above moves the string '??' into table
element number 1, beginning at character position 3, for a length of 2
characters.
- We will cover reference modification (3 : 2) later in this course
|
|
24
|
- Each subscript/index must be either
- A positive integer
- A data name which represents a positive integer
- A simple expression which evaluates to a positive integer
- The subscript/index must contain a value between 1 and the number of
elements in the table/array inclusive.
- When more than one subscript/index is used they must be separated from
one another by commas.
- One subscript/index must be specified for each dimension of the table.
- 1 subscript/index for a one dimension table
- 2 subscripts/indices for a two dimension table
- 3 subscripts/indices for a three dimension table
-
- Subscripts and indexes must be enclosed in parentheses/brackets
- Indexes are manipulated using the COBOL SET keyword:
- SET QTY-IDX TO 1.
- SET QTY-IDX UP BY 1.
- SET QTY-IDX DOWN BY 1.
|
|
25
|
- 01 Error-Flag-Table Value Spaces.
- 88 No-Errors Value Spaces.
- 05 Type-Error PIC X.
- 05 Shift-Error PIC X.
- 05 Home-Code-Error PIC X.
- 05 Work-Code-Error PIC X.
- 05 Name-Error PIC X.
- 05 Initials-Error PIC X.
- 05 Duplicate-Error PIC X.
- 05 Not-Found-Error PIC X.
- 01 Filler Redefines Error-Flag-Table.
- 05 Error-Flag Occurs 8 Times
- 77 ERROR-ON PIC X Value "E".
- Indexed By
Flag-Index PIC X.
- ***********************************************************
- 01 Error-Message-Table.
- 05 Filler PIC X(25) Value "Transaction
Type Invalid".
- 05 Filler PIC X(25) Value "Shift Code
Invalid".
- 05 Filler PIC X(25) Value "Home Location
Code Inval.".
- 05 Filler PIC X(25) Value "Work Location
Code Inval.".
- 05 Filler PIC X(25) Value "Last Name -
Blanks".
- 05 Filler PIC X(25) Value "Initials -
Blanks".
- 05 Filler PIC X(25) Value "Duplicate
Record Found".
- 05 Filler PIC X(25) Value "Commuter Record
Not Found".
- 01 Filler Redefines Error-Message-Table.
- 05 Error-Message Occurs 8 Times
- Indexed By
Message-Index PIC X(25).
|
|
26
|
- From the course workshop documents, do the following labs:
- Data Representation and assignment (MOVE statement) Lab
- Open ended workshop
|
|
27
|
|
|
28
|
- By the end of this unit you should be able to:
- Describe the COBOL OCCURS clause and code a syntactically correct
example for a two-dimension table
- Create COBOL two-dimension tables, by redefining values in
Working-Storage
- Initialize and load data in a two-dimension COBOL table
- Refer to specific table row/field table occurrences in the Procedure
Division of two dim tables
|
|
29
|
- Many organizational patterns for data require the use of
multi-dimensional arrays to model information accurately.
- Consider the following:
- Historical data
- Accounting period data
- University data
- Corporate data
- Territories
- Projects
- Locations
- Geographic data
|
|
30
|
- To create a COBOL data structure that is a two-dimension array simply
define an OCCURS within an OCCURS variable definition
- From the snapshot visual-example note:
- SAMPLE-TABLE-TWO is the highest group data item
- TABLE-ROW is the outer occurring group
- TABLE-COLUMN is the inner occurring group. It contains two elementary fields:
- TABLE-ITEM-1 elementary field data
- TABLE-ITEM-2 elementary field data
- You may have elementary or group data-items at any level within the
two-dim table.
|
|
31
|
- 01 GENERIC-TWO-DIMENSION-TABLE-STRUCTURE.
- 05 DIM-FIRST-LEVEL OCCURS 100 TIMES.
- 10 FIRST-LEVEL-ELEMENTARY-DATA PIC X(20)
- 10 DIM-2ND-LEVEL OCCURS 100 TIMES.
- 20
ELEMENTARY-ITEMS-2ND-LEVEL.
- 30 ELEMENTARY-1 PIC X(10).
- 30 ELEMENTARY-2 PIC X(07).
- 30 ELEMENTARY-3 PIC X(16).
- 01 STUDENT-GRADES.
- 05 CLASSES-TAKEN OCCURS 20 TIMES.
- 10 CLASS-DETAILS OCCURS 10 TIMES.
- 20 CLASS-NBR PIC X(10).
- 20 GRADE-VAL PIC X(2).
- 01 COMPANY-ORG-THREE-DIM-TABLE.
- 05 COMPANY-LOCATION OCCURS 4 TIMES.
- 10 LOCATION-ID PIC X(05).
- 10 TERRITORY-TABLE OCCURS 13 TIMES.
- 20 TERRITORY-NAME PIC X(20).
- 20 TERRITORY-REP OCCURS 4 TIMES.
- 30 PRIVATE-DATA.
- 40 ADDRESS PIC X(20).
- 40 CURRENT-ASSIGNMENT PIC X(60).
|
|
32
|
- As in a single-dimension COBOL table, you can load a
Two-or-Three-Dimension table:
- Programmatically (from screen, file or database values)
- REDEFINE constant field values with an OCCURS (previous slides)
- Using Initialize see example below
- Using the VALUE clause on elementary field definitions.
- The syntax and rules are the same for all-of-the-above methods:
- Examples,
- INITIALIZE TABLE-ONE REPLACING NUMERIC DATA BY 3.
- INITIALIZE TABLE-ONE REPLACING ALPHANUMERIC DATA BY "X".
- For loading a table programmatically, you will nest PERFORM statements
within PERFORM statements (see next slide)
- Note also that nesting PERFORM statements is far & away the most
common and flexible method of dynamically loading a table
- The rules for referring to multi-dimension COBOL tables are the same as
single-dimension OCCURS tables and described on the next slide
|
|
33
|
- Note the following:
- The PERFORM procedure nesting that mirrors the table structure nesting.
- You reference elementary data in the outer table dimension with a
single subscript or index
- FIRST-LEVEL-ELEMENTARY-DATA (DIM-1-IDX)
- You reference elementary data in the inner table with multiple
subscripts or indices, separated by commas
- ELEMENTARY-2 (DIM-1-IDX, DIM-2-IDX)
- Not shown you could also refer to group level data at any level
inside the table (with the appropriate indices)
|
|
34
|
- From the course workshop documents, do the following labs:
- Data Representation and assignment (MOVE statement) Lab
- Open ended workshop
|
|
35
|
|
|
36
|
- By the end of this unit you should be able to:
- Describe the concept behind COBOL variable-length OCCURS clause and code
a syntactically correct example for variable length (Occurs Depending
On) tables
- Create COBOL variable-length tables
- Initialize and load data in variable length COBOL table
- Refer to specific table row/field table occurrences in the PROCEDURE
DIVISION for variable length COBOL table data
|
|
37
|
- COBOL permits table definitions that occur an unknown-until-run-time
or variable number of times
- The exact number of table-row occurrences depends on the value in some
other field in (typically) WORKING STORAGE.
- For example here is a purchase-order record:
- 01 PURCHASE-ORDER.
- 05 NAME-ADDR-INFO PIC X(100).
- 05 LINE-ITEM-OCCURRENCES PIC 999.
-
- 05 LINE-ITEMS-TABLE
- OCCURS 0 TO 100 TIMES
DEPENDING ON LINE-ITEM-OCCURENCES.
10
ITEM-ID
PIC 9999.
- 10 ITEM-NAME PIC X(30).
10
ITEM-DESCRIP PIC X(30).
10
ITEM-UNIT-PRICE PIC S9(7)V99.
-
- Notes:
- The table LINE-ITEMS-TABLE contains four fields (Item-ID, Item-Name,
Item-Description, Item-Unit-Price) which make up one line on a Purchase
Order.
- Each Purchase Order can have from 0 to 100 line items.
- The actual number of line items for any given Purchase Order is read
from a file, and given by the value of the field LINE-ITEM-OCCURRENCES.
|
|
38
|
- If you do not know before run-time how many times a table element
occurs, define a variable-length table.
- To do so, use the OCCURS DEPENDING ON clause.
- X OCCURS 1 TO 10 TIMES DEPENDING ON Y
- In the example above,
- X is called the ODO subject
- Y is called the ODO object.
- Two factors affect the successful manipulation of variable-length
records:
- 1. Correct calculation of record lengths T
- The length of the variable portions of a group item is the product of
the
- Object of the DEPENDING ON phrase plus the
- Length of the subject of the
OCCURS clause.
- In the example above, if X is a field or group of length 50 bytes, and
Y is equal to 9 (based on the run-time value of the field name Y), the
total length of the table is 450 bytes
- 2. Conformance of the data in the object of the OCCURS DEPENDING ON
clause to its PICTURE clause
- If the content of the ODO object does not match its PICTURE clause,
the program could terminate abnormally.
- You must ensure that the ODO object correctly specifies the current
number of occurrences of table elements.
|
|
39
|
- The following example shows a group item (REC-1) that contains both the
subject and object of the OCCURS DEPENDING ON clause.
- The way the length of the group item is determined depends on whether it
is sending or receiving data.
- WORKING-STORAGE SECTION.
- 01 MAIN-AREA.
- 03 REC-1.
- 05 FIELD-1 PIC 9.
- 05 FIELD-2 OCCURS 1 TO 5 TIMES DEPENDING ON FIELD-1 PIC X(05).
- 01 REC-2.
- If you want to move REC-1 (the sending item in this case) to REC-2, the
length of REC-1 is determined immediately before the move, using the
current value in FIELD-1.
- If the content of FIELD-1 conforms to its PICTURE clause (that is, if
FIELD-1 contains a zoned decimal item), the move can proceed based on
the actual length of REC-1.
- Otherwise, the result is unpredictable.
- You must ensure that the ODO object has the correct value before you
initiate the move.
- When you do a move to REC-1 (the receiving item in this case), the
length of REC-1 is determined using the maximum number of occurrences.
In this example, five occurrences of FIELD-2, plus FIELD-1, yields a
length of 26 bytes.
- In this case, you do not need to set the ODO object (FIELD-1) before
referencing REC-1 as a receiving item.
- However, the sending field's ODO object (not shown) must be set to a
valid numeric value between 1 and 5 for the ODO object of the receiving
field to be validly set by the move.
|
|
40
|
- 01 Client-Transactions.
- 05
Client-Name
PIC X(30).
- 05
Client-SS-Number
PIC 9(9).
- 05 Number-of-Transactions PIC 99
COMP-3.
- 05 Transaction-History OCCURS 0 TO 50 TIMES
- DEPENDING ON Number-of-Transactions INDEXED BY Trans-IDX.
- 10 Transaction-Date.
- 15 Transaction-DAY
PIC 99.
- 15 Transaction-MONTH PIC 99.
- 15 Transaction-YEAR PIC
9(4).
- 10
Transaction-Description PIC X(300).
- 10
Transaction-Type PIC 99.
|
|
41
|
- Read a record from a file.
- The record contains:
- The data for the ODO table
- The number of occurrences in the ODO table
- Perform a routine that moves the data from the file into the ODO table
row until the max table-length (in number of occurrences for the new row
as recorded in the record) is hit
- Read the next file/record
|
|
42
|
- Complex ODO is supported as an extension to Standard COBOL 85.
- Several types of complex OCCURS DEPENDING ON (complex ODO) are possible.
- The basic forms of complex ODO permitted by the compiler are as follows:
- Variably located item or group:
- A data item described by an OCCURS clause with the DEPENDING ON phrase
is followed by a non-subordinate elementary or group data item.
- Variably located table:
- A data item described by an OCCURS clause with the DEPENDING ON phrase
is followed by a non-subordinate data item described by an OCCURS
clause.
- Table that has variable-length elements:
- A data item described by an OCCURS clause contains a subordinate data
item described by an OCCURS clause with the DEPENDING ON phrase.
- Index name for a table that has variable-length elements.
- Element of a table that has variable-length elements.
|
|
43
|
- 01 FIELD-A.
- 02 COUNTER-1 PIC S99.
- 02 COUNTER-2 PIC S99.
- 02 TABLE-1.
- 03 RECORD-1 OCCURS 1 TO 5 TIMES DEPENDING ON COUNTER-1 PIC X(3).
- 02 EMPLOYEE-NUMBER PIC X(5).
- (1) 02 TABLE-2 OCCURS 5 TIMES
- (2)(3) INDEXED BY INDX.
- (4) 03 TABLE-ITEM PIC 99.
- (5) 03 RECORD-2 OCCURS 1 TO 3 TIMES DEPENDING ON COUNTER-2.
- 04 DATA-NUM PIC S99.
- In this example, COUNTER-1 is an ODO object, that is, it is the object
of the DEPENDING ON clause of RECORD-1. RECORD-1 is said to be an ODO
subject. Similarly, COUNTER-2 is the ODO object of the corresponding ODO
subject, RECORD-2.
- The types of complex ODO occurrences shown in the example above are as
follows:
- (1) - A variably located item: EMPLOYEE-NUMBER is a data item that
follows, but is not subordinate to, a variable-length table in the same
level-01 record.
- (2) - A variably located table: TABLE-2 is a table that follows, but is
not subordinate to, a variable-length table in the same level-01
record.
- (3) - A table with variable-length elements: TABLE-2 is a table that
contains a subordinate data item, RECORD-2, whose number of occurrences
varies depending on the content of its ODO object.
- (4) - An index-name, INDX, for a table that has variable-length
elements.
- (5) - An element, TABLE-ITEM, of a table that has variable-length
elements.
|
|
44
|
- From the course workshop documents, do the following labs:
- Data Representation and assignment (MOVE statement) Lab
- Open ended workshop
|
|
45
|
|
|
46
|
- By the end of this unit you should be able to:
- Describe the COBOL SEARCH, SEARCH ALL and PERFORM VARYING processing for
doing table searches
- Code a syntactically correct SEARCH and SEARCH ALL statement
- Describe the requirements for SEARCH ALL
- Define COBOL performance trade-offs with the above three methods of
table searches
|
|
47
|
- There are three ways to search through COBOL tables for values you want
to find:
- Perform Varying
- You've already seen this essentially you manipulate an index or
subscript within an inline PERFORM, or paragraph PERFORM
- Sequential SEARCH
- COBOL has a reserved word (SEARCH) that automatically will search
through a table starting from a pre-determined element, and going
until:
- SEARCH finds a match
- The code hits an end-of-table condition
- Binary SEARCH ALL
- COBOL also has a reserved phrase: SEARCH ALL that does a binary search
through a COBOL table, until:
- SEARCH ALL finds a match
- The code hits an end-of table condition
- Details on SEARCH ALL upcoming
- All three search coding idioms can accomplish the same technical result
finding data in table, but all three are very different in:
- Syntax
- Data pre-req's
- Run-time performance (efficiency)
|
|
48
|
- 77 State-Search-Data PIC X(20).
- 01 States-Table-Seq.
- 05 State-Name OCCURS 50 TIMES
- INDEXED BY St-Idx-Seq PIC
X(20).
- 01 States-Table-Bin.
- 05 State-Name-Bin OCCURS 50 TIMES
- Ascending key is State-Name-Bin
- INDEXED BY St-Idx-Bin PIC
X(20).
-
- Procedure Division.
-
- *** Option 1. PERFORM VARYING ****
- PERFORM VARYING St-idx-Seq from 1 by 1 Until St-idx > 50
- If State-Search-Data = State-Name (St-Idx-Seq)
-
- *** Option 2. SEQUENTIAL SEARCH ****
- Set St-Idx-Seq to 1.
- SEARCH States-Table Varing St-Idx-Seq
- At End
- When State-Search-Data = State-Name(St-Idx-Seq)
-
- *** Option 3. BINARY SEARCH ALL ****
- SEARCH ALL States-Table
- At End
- When State-Name(St-Idx-Bin) = State-Search-Data
-
|
|
49
|
- Use the SEARCH statement to do a serial (sequential) search beginning at
the current index setting.
- To modify the index setting before searching, use the SET statement.
- Other considerations:
- The conditions in the WHEN phrase are evaluated in the order in which
they appear:
- If none of the conditions is satisfied, the index is increased to
correspond to the next table element, and the WHEN conditions are
evaluated again.
- If one of the WHEN conditions is satisfied, the search ends.
- The index remains pointing to the table element that satisfied the
condition.
- If the entire table has been searched and no conditions were met, the AT
END imperative statement is executed if there is one.
- If you did not code AT END, control passes to the next statement in the
program.
- You can reference only one level of a table (a table element) with each SEARCH
statement.
- To search multiple levels of a table, use nested SEARCH statements.
- Delimit each nested SEARCH statement with END-SEARCH.
- See Notes on Performance Coding Considerations
|
|
50
|
- 01 SALES-TAX.
- 05 FILLER PIC X(21) VALUE '280000005463968869950'.
- 05 FILLER PIC X(21) VALUE '329650006543603457350'.
- 05 FILLER PIC X(21) VALUE '557450007573101219150'.
- 05 FILLER PIC X(21) VALUE '7269000087652800363750'.
- 05 FILLER PIC X(21) VALUE '902650005431500000001'.
- 01 SALES-TAX-TABLE REDEFINES SALES-TAX.
- 05 SALES-TAX-ITEM OCCURS 5 TIMES
- INDEXED BY TX-IDX.
- 10 SALES-TAX-L-RANGE PIC
9(6)V9(2).
- 10 SALES-TAX-RATE PIC V9(6).
- 10 SALES-TAX-H-RANGE PIC
9(5)V9(2).
- PROCEDURE DIVISION.
- SEARCH ALL SALES-TAX-ITEM
- AT END
- PERFORM 700-INVALID-TAX-ID
- WHEN SALES-TAX-L-RANGE (TX-IDX) > IN-ITEM-TAX-ID
- PERFORM 200-COMPUTE-WITH-TAX-TOTALS.
|
|
51
|
- The following example shows how you might find a particular string in
the innermost table of a three-dimensional table.
- Each dimension of the table has its own index
- Set to 1, 4, and 1, respectively
- The innermost table
- TABLE-ENTRY3 has an ascending key
|
|
52
|
- With SEARCH ALL to do a binary search, you do not need to set the index
before you begin.
- The index is always the one that is associated with the first
index-name in the OCCURS clause.
- The index varies during execution to maximize the search efficiency.
- To use the SEARCH ALL statement to search a table, the table must
specify the ASCENDING or DESCENDING KEY phrases of the OCCURS clause, or
both, and must already be ordered on the key or keys that are specified
in the ASCENDING and DESCENDING KEY phrases:
- Table-Rows OCCURS 50 Times ASCENDING KEY IS Key-Field Indexed by T-IDX
- In the WHEN phrase of the SEARCH ALL statement, you can test any key
that is named in the ASCENDING or DESCENDING KEY phrases for the table
- The key portion of the comparison must follow after WHEN (see example
below)
- You must test all preceding keys.
- The test must be an equal-to condition, and the WHEN phrase must
specify either a key (subscripted by the first index-name associated
with the table) or a condition-name
(88 level) that is associated with the key.
- The WHEN condition can be a compound condition that is formed from
simple conditions that use AND as the only logical connective.
- Each key and its object of comparison must be compatible according to
the rules for comparison of data items.
|
|
53
|
- 01 SALES-TAX.
- 05 FILLER PIC X(18) VALUE '280000003968869950'.
- 05 FILLER PIC X(18) VALUE '329650003603457350'.
- 05 FILLER PIC X(18) VALUE '557450003101219150'.
- 05 FILLER PIC X(18) VALUE '726900002800363750'.
- 05 FILLER PIC X(18) VALUE '902650001500000000'.
- 01 SALES-TAX-TABLE REDEFINES SALES-TAX.
- 05 SALES-TAX-ITEM OCCURS 5 TIMES
- ASCENDING KEY IS SALES-TAX-L-RANGE
- INDEXED BY TX-IDX.
- 10 SALES-TAX-L-RANGE PIC
9(6)V9(2).
- 10 SALES-TAX-RATE PIC V9(3).
- 10 SALES-TAX-H-RANGE PIC
9(5)V9(2).
- PROCEDURE DIVISION.
- SEARCH ALL SALES-TAX-ITEM
- AT END
- PERFORM 700-INVALID-TAX-ID
- WHEN SALES-TAX-L-RANGE (TX-IDX) = IN-ITEM-TAX-ID
- PERFORM 200-COMPUTE-WITH-TAX-TOTALS.
|
|
54
|
- The following example The following example shows how you can code a
binary search of a table.
- Suppose you define a table that contains 90 elements of 40 bytes each,
and three keys.
- The primary and secondary keys
- (KEY-1 and KEY-2) are in ascending order, but the least significant key
(KEY-3) is in descending order:
- If an entry is found in which each of the three keys is equal to the
value to which it is compared
- (VALUE-1, VALUE-2, and VALUE-3, respectively)
- PART-1 of that entry is moved to OUTPUT-AREA.
- If no matching key is found in the entries in TABLE-A, the NOENTRY
routine is performed.
|
|
55
|
- From: http://www-949.ibm.com/software/rational/cafe/thread/2200?tstart=0
- Of the three table search options:
- PERFORM Loops are the slowest option
- SEARCH ALL is fastest, but only for tables > 50-some elements
(interesting)
- SEARCH (sequentially) is faster than SEARCH ALL for small tables, and
considerably slower for very large tables
- A Perform loop (especially an inline one) may well be FASTER than some SEARCH
statements. This probably depends on what you are varying.
- SEARCH ALL beats SEARCH (sequential) but SEARCH ALL does require your
table to be sorted (already).
- SEARCH ALL only permits WHEN conditions that test for equality
- If your "input" isn't sorted (or however you build the table),
then you need to consider the overhead to sort it before deciding
whether to do a sequential SEARCH or a SEARCH ALL.
- If you search a large table many times, then doing the sort is probably
a good idea. IBM provides a "nifty" way to do this - if you
aren't familiar with the technique.
- Check out: Http://publibfp.boulder.ibm.com/cgi-bin/bookmgr/BOOKS/igy3pg40/1.12.10.2
|
|
56
|
|
|
57
|
- You can use intrinsic functions to process alphabetic, alphanumeric,
national, or numeric table items.
- You can process DBCS data items only with the NATIONAL-OF intrinsic
function.
- The data descriptions of the table items must be compatible with the
requirements for the function arguments.
- Use a subscript or index to reference an individual data item as a
function argument.
- For example, assuming that Table-One is a 3 x 3 array of numeric items,
you can find the square root of the middle element by using this
statement:
- Compute X = Function Sqrt(Table-One(2,2))
- You might often need to iteratively process the data in tables.
- For intrinsic functions that accept multiple arguments, you can use the
subscript ALL to reference all the items in the table or in a single
dimension of the table.
- The iteration is handled automatically, which can make your code shorter
and simpler.
- You can mix scalars and array arguments for functions that accept
multiple arguments:
- Compute Table-Median = Function Median(Arg1 Table-One(ALL))
|
|
58
|
- These examples show how you can apply an intrinsic function to some or
all of the elements in a table by using the ALL subscript.
- Assuming that Table-Two is a 2 x 3 x 2 array, the following statement
adds the values in elements
- Table-Two(1,3,1)
- Table-Two(1,3,2)
- Table-Two(2,3,1)
- Table-Two(2,3,2)
- Compute Table-Sum = FUNCTION SUM (Table-Two(ALL, 3, ALL))
- The following example computes various salary values for all the
employees whose salaries are encoded in Employee-Table:
|
|
59
|
|