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 B
Trang 1IBM Software Group
© 2006 IBM Corporation
Enterprise COBOL Education Using Rational Developer for System Z
Intermediate COBOL Record and Table Handling Patterns
Jon Sayles, IBM Software Group, Rational EcoSystems Team
Trang 2IBM Trademarks and Copyrights
© 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 IBM’s 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.
Trang 3Course Contributing Authors
Thanks to the following individuals, for assisting with this course:
David Myers/IBM
Trang 4Purpose of This Document
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
Trang 5 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.
Trang 6Unit
Additional Record Concepts Additional Record Concepts
COBOL Tables – Definition and Language Concepts
Multi-Dimension COBOL Tables
Variable Length COBOL Tables
Searching COBOL Tables
Processing COBOL Tables With Intrinsic Functions
Topics:
Additional Record and COBOL Table Handling Facilities
Trang 7COBOL Records – Review
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
01 Employee-Data.
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)
Trang 8REDEFINED Fields
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
A 3 4 T 9 7 b b b b b b b b b b b b b b b b b b b b b b b b b Postal-Box Filler
Trang 9Defining and Using REDEFINED Fields
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
01 DATE-TABLE.
05 DATEVALS PIC X(30) VALUE "312831303130313130313031".
05 Month-Days REDEFINES DATEVALS PIC 9(2) OCCURS 12
Trang 10REDEFINED Records
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
… Perform Detail Record Processing
Trang 11Lab Assignment
From the course workshop documents, do the following labs:
1 Data Representation and assignment (MOVE statement) Lab
2 Open ended workshop
Trang 12Unit
Additional Record Concepts
COBOL Tables – Definition and Language Concepts
Multi-Dimension COBOL Tables
Variable Length COBOL Tables
Searching COBOL Tables
Processing COBOL Tables With Intrinsic Functions
Topics:
Additional Record and COBOL Table Handling Facilities
Trang 13Topic Objectives
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
Trang 14COBOL Tables – Overview
Like most programming languages, COBOL can group variables of the same type
in internal program tables
COBOL supports the following kinds of table structures:
1 Single COBOL field tables
2 Fixed-length Structure field tables – the most common format
3 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: OCCURS
– Rows 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
Fixed-Length COBOL Structure Table
Three occurrences
of two fields
…
Trang 15COBOL Tables – Overview
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 S0C4 –
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
A COBOL table of 3
dimensions
Trang 16Why COBOL Tables?
There are generally two reasons for tables in any language, including COBOL:
1 To simplify repetitive processing patterns and organize related repetitive data
2 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:
* Consider the math to total the above
* …and worse? The quarterly maintenance
* to the data and computations and routines
03 NBR-YEARS PIC S9(4) COMP.
03 QRTRLY-Table OCCURS 0 TO 40 TIMES
DEPENDING ON NBR-YEARS INDEXED BY QTRLY-INDEX.
Trang 17What can you do with COBOL Tables?
There are four basic categories of things to learn about COBOL tables :
1 Define COBOL tables
Most of the time in WORKING-STORAGE
Specific syntax examples follow
2 Load COBOL tables
From files or databases
Programmatically from within the PROCEDURE DIVISION
3 Initialize and re-initialize COBOL tables
4 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
Trang 1805 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 table-name
– 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 INDEXED BY will create an internal subscript you will use to reference table occurrences
– Using INDEXED BY can be substantially more efficient than using subscripts – INDEXED BY
particularly for large tables (> 1,000 rows)
Trang 19Define One-Dimension COBOL Tables – Examples
05 REGION OCCURS 4 TIMES.
10 Q1-SALES PIC 9(5)V99 VALUE ZEROES.
10 Q2-SALES 10 Q2-SALES PIC 9(5)V99 VALUE ZEROES.
10 Q3-SALES 10 Q3-SALES PIC 9(5)V99 VALUE ZEROES.
10 Q4-SALES 10 Q4-SALES PIC 9(5)V99 VALUE ZEROES.
05 FILLER PIC X(09) VALUE "SUNDAY ".
05 FILLER PIC X(09) VALUE "MONDAY " 05 FILLER PIC X(09) VALUE "MONDAY ".
05 FILLER PIC X(09) VALUE "TUESDAY " 05 FILLER PIC X(09) VALUE "TUESDAY ".
05 FILLER PIC X(09) VALUE "WEDNESDAY" 05 FILLER PIC X(09) VALUE "WEDNESDAY".
05 FILLER PIC X(09) VALUE "THURSDAY " 05 FILLER PIC X(09) VALUE "THURSDAY ".
05 FILLER PIC X(09) VALUE "FRIDAY " 05 FILLER PIC X(09) VALUE "FRIDAY ".
05 FILLER PIC X(09) VALUE "SATUDAY " 05 FILLER PIC X(09) VALUE "SATUDAY ".
01 Weekday-Table REDEFINES WEEK-DAY-VALUES
05 WS-Day-of-Week PIC X(09) OCCURS 7 TIMES.
Examples of one-dimension COBOL table loaded by REDEFINE-ing hard-coded
variable values
Trang 20Load Tables Using Initialize – Syntax
You can load a table:
– Programmatically (from screen, file or database values) – next topic
– REDEFINE constant field values with an REDEFINE OCCURS (previous slides) OCCURS
– Using Initialize – see example below Initialize
– 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 3
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 X
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 INITIALIZE
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 REPLACING INITIALIZE statement similarly to initialize all of INITIALIZE
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 INITIALIZE
A table that was defined using OCCURS DEPENDING ON
Trang 21Load Tables Using INITIALIZE – Example INITIALIZE
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 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:
Note – VALUE clauses
initially load the table with specific variable data
Trang 22Load Tables Dynamically
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) 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
WORKING-STORAGE SECTION
…
77 PROJECT-INDEX PIC S9(4) COMP.
77 TABLE-MAX PIC S9(4) COMP VALUE 20.
77 SW-END-OF-FILE PIC X(01) VALUE SPACES.
88 END-OF-FILE VALUE 'Y'.
… READ INPUT-FILE AT END MOVE 'Y' TO
EMP-NAME (PROJECT-INDEX) READ INPUT-FILE AT END MOVE 'Y' TO
SW-END-OF-FILE END-PERFORM.
…What's the deal with Perform Varying?
Trang 23Referring to Values in a One-Dimension COBOL Table - Considerations
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 sub string 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 (3 : 2) later in this course
Trang 24Rules for Subscripts and Indexes
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.
Trang 25Putting it All Together – a
complete example
01 Error-Flag-Table Value Spaces.
88 No-Errors Value Spaces.
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).
PROCEDURE DIVISION PROCEDURE DIVISION.
To Print-Message Perform 260-Print-Report
End-If End-Perform
Trang 26Referring to a One-Dimension COBOL Table in the Procedure Division
From the course workshop documents, do the following labs:
1 Data Representation and assignment (MOVE statement) Lab
2 Open ended workshop
Trang 27 Additional Record Concepts
COBOL Tables – Definition and Language Concepts
Multi-Dimension COBOL Tables
Variable Length COBOL Tables
Searching COBOL Tables
Processing COBOL Tables With Intrinsic Functions
Topics:
Additional Record and COBOL Table Handling Facilities
Trang 28Topic Objectives
By the end of this unit you should be able to:
Describe the COBOL OCCURS clause and code a syntactically correct example for a dimension table
two- 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
Trang 29Multiple Dimension COBOL Tables
Many organizational patterns for data require the use of multi-dimensional arrays to model information accurately.
Consider the following:
One month of 4 weeks of 7 days
One week of 7 days of many hours