Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
http://www-306.ibm.com/software/awdtools/cobol/zos/library/
ASSIGN assignment-name
TO
Environment Division
File-CONTROL
• Optional – used for files opened in I-O, INPUT,
or EXTEND. File doesn’t have to be present
when the program is executed.
• File-name1 – identifies an FD entry (internal file
name)
• Assignment-name – identifies the external file. If
name component of the SELECT clause is found
in the JCL it is treated as a DD name. If not
found in the JCL, then “name” is treated an an
environment variable
QSAM File Name
name
label- S-
• Label – documents for the programmer the device and device class to
which the file is assigned. No effect on execution. Must end with a dash
• S – Optional. Indicates sequential organization
Environment Variables
Exercise A (Dynamic Files)
• Statically allocate and read
BCST.SICCC01.TESTPDS(DYNAMDAT)
• This file contains member names of other members in
BCST.SICCC01.TESTPDS
• Dynamically Read each member that is listed and
display the records in each member
• After you can display all the records, try writing out the
records to a dynamically allocated file
• Use BCST.SICCC01.PDSLIB(DYNAM2) to help you
read a file dynamically
• Use BCSC.SICCC01.PDSLIB(DYNAM1) to help you
write a file dynamically
Environment Variables
• Defined as WORKING-STORAGE fields
using value clauses
RESERVE integer
AREA
AREAS
RESERVE Clause
• Specifies the number of I/O buffers
allocated the file at run-time
• If omitted, the number of buffers is taken
from the DD statement. If none are
specified, the system default is taken
QSAM Buffering
• QSAM buffers can be allocated above the 16 MB line if
all of the following are true:
- Enterprise COBOL
- z/OS Language Environment
- the programs are compiled with RENT and DATA(31)
or
compiled with NORENT and | RMODE(ANY)
- the program is executing in AMODE 31
- the program is executing on MVS
- the ALL31(ON) run-time option is used (for
EXTERNAL files)
ORGANIZATION Clause (optional)
• ORGANIZATION IS SEQUENTIAL
• Other non-QSAM options: INDEXED,
RELATIVE, LINE SEQUENTIAL
• Records are read and written in a serial
manner
PADDING Clause
PADDING data name
CHARACTER IS literal
- The operating system moves a value to dname1 and possibly dname2 after
each I/O operation.
-dname1 - a two character alphanumeric or national field
-dname2 – used for VSAM
Environment Division
I-O-CONTROL
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT …
I-O-CONTROL.
APPLY WRITE-ONLY ON MYFILE.
12345
54321 Joe
Betty
Brown
Smith 10
30 20
40
LOC-CODE(3)
NAME(3)
EMP-NO(4)
Exercise #9
• Implement a single dimension table of
days. Print the table from beginning to
end
• Turn the table into a “fat” table by adding a
column with the number of letters in each
day name.
• Print each day name and the number of
letters it contains.
Multi-Dimension Tables
• COBOL supports up to 7 dimensions in
tables
• Use OCCURS within OCCURS to add
multiple dimensions
• 01 EMP-TABLE
05 EMPLOYEE OCCURS 100 TIMES.
10 NAME PIC X(30).
10 HOURS PIC S99 OCCURS 7 TIMES.
Multi-Dimension Table
• 01 EMP-TABLE.
05 EMPLOYEE OCCURS 3 TIMES.
10 NAME PIC X(30).
10 HRS PIC S99 OCCURS 3 TIMES.
EMPLOYEE(3)
Exercise #10
• Create a table of integers with 4 rows and
5 columns.
• Print the table row by row
• Print the table column by column
• Compute and print the sum of each row
• Compute and print the sum of each
column
• Compute and print the sum of all entries in
the table
Creating Tables with Indexes
• 01 EMPLOYEE –TABLE.
05 EMPLOYEE OCCURS 100 TIMES
INDEXED BY I,J.
• 01 SALES-TABLE.
05 MONTH-RECORD OCCURS 12 TIMES
INDEXED BY M.
10 NAME PIC X(30).
2O AMOUNT PIC 9(5)V99 PACKED-DECIMAL
OCCURS 31 TIMES
INDEXED BY D.
Subscripts vs Indexes
• Subscripts
– Represent an occurrence number
– User defined as a numeric field – best to
choose USAGE IS BINARY
– Printable (since they are numeric)
– Can use relative subscripts J+1 or J-3
– Manipulated with PERFORM loops,
assignments, and arithmetic commands
Subscripts vs Indexes
• Indexes
– Represent a displacement value from the start
of a table.
– More efficient than subscripts
– Created automatically when a table is defined
with indexes
– Not really designed to be printed
– Manipulated with PERFORM loops, and SET
statements
SET Statements
• Examples
– SET J TO K
– SET J TO 1
– SET K UP BY 1
– SET K DOWN BY 1
– SET K TO K + 1
Exercise #11
• Convert Exercise #10 so that you are
using indexes instead of subscripts
Sequential Search
• COBOL provides a SEARCH command
that provides a sequential search for
tables that have indexes
• Table entries do not have to be sorted
• AT END clause provides code in the
situation that the search is unsuccessful
• Searching starts with the current index
value
SEARCH
Sequential Searching
• 01 EMPLOYEE-TABLE.
05 EMPLOYEE OCCURS 100 TIMES
INDEXED BY I-NDX.
10 EMP-NO PIC 9(5).
10 EMP-RANK PIC X(5).
…
SET I-NDX TO 1
SEARCH EMPLOYEE
AT END
DISPLAY ‘NOT FOUND’
WHEN EMP-NO(I-NDX) = 12345
DISPLAY EMP-RANK(I-NDX)
END-SEARCH
Sequential Searching
• 01 EMPLOYEE-TABLE.
05 EMPLOYEE OCCURS 100 TIMES
INDEXED BY I-NDX.
10 EMP-NO PIC 9(5).
10 EMP-RANK PIC X(5).
…
SET I-NDX TO 1
SEARCH EMPLOYEE
AT END
DISPLAY ‘NOT FOUND’
WHEN EMP-NO(I-NDX) < 10000
DISPLAY EMP-RANK(I-NDX)
WHEN EMP-NO(I-NDX) > 2000
DISPLAY EMP-RANK(I-NDX)
END-SEARCH
Sequential Search
• Search can be continued in a loop after
setting index value up or down
SET I-NDX TO 1
PERFORM UNTIL …
SEARCH EMPLOYEE
AT END
DISPLAY ‘NOT FOUND’
WHEN EMP-NO(I-NDX) = 12345
DISPLAY EMP-RANK(I-NDX)
SET I-NDX UP BY 1
END-SEARCH
END-PERFORM
Exercise #12
• Create a fat single dimension table with
the data in the file DATA1. Read the file
and store the second (Item #) and third
fields (Item name) in the table.
• Assume a fixed size table of 40 items.
• Sequentially search the table for item #s in
the range 400 to 450. Print out the results
of the search.
Binary Searching
• Entire table is searched. No need to initialize an
index
• Table must have an ASCENDING or
DESCENDING KEY IS clause. Table must be
sorted.
• Only one WHEN clause allowed. The WHEN
clause is comprised of one or more “equal” tests
joined by AND operators
• AT END clause is invoked if the WHEN clause is
never satisfied
Binary Search
Binary Searching
• 01 EMPLOYEE-TABLE.
05 EMPLOYEE OCCURS 100 TIMES
ASCENDING KEY IS EMP-NO
INDEXED BY I-NDX.
10 EMP-NO PIC 9(5).
10 EMP-RANK PIC X(5).
…
SEARCH ALL EMPLOYEE
AT END
DISPLAY ‘NOT FOUND’
WHEN EMP-NO(I-NDX) = 12345
DISPLAY EMP-RANK(I-NDX)
END-SEARCH
SEARCH ALL
• SEARCH ALL performs a binary search with an index
• ENTRIES MUST BE IN ORDER
• No SET necessary (whole table searched)
01 SALES-TAX.
05 TAB-ENTRIES OCCURS 100 TIMES
ASCENDING KEY ZIPCODE
INDEXED BY K.
10 ZIPCODE PIC 9(5).
10 RATE PIC V999.
FD CUSTFILE
RECORD IS VARYING IN SIZE
FROM 50 TO 80 CHARACTERS
DEPENDING ON RECSIZE.
When a record is read from a file defined with the RECORD IS
VARYING IN SIZE.. DEPENDING ON identifier phrase, the size of
the record read into the buffer is moved into the data-item identifier
INSPECT MYSTRING
REPLACING ALL “XXXX" BY “ABCD“
AFTER INITIAL “A“
BEFORE INITIAL “P"
TALLYING … REPLACING
INSPECT LINE TALLYING ACOUNT
FOR ALL “A”
REPLACING ALL “X” BY “Y"
AFTER INITIAL “A"
BEFORE INITIAL “Z“
CONVERTING
INSPECT MYTEXT
CONVERTING
"abcdefghijklmnopqrstuvwxyz“
TO
"ABCDEFGHIJKLMNOPQRSTUVWXYZ“
Pointers
Creating a Pointer
• 05 PTR USAGE IS POINTER.
• 05 A-PTR POINTER.
• These definitions create 4 byte fullwords
capable of containing addresses of
memory locations
Setting a Pointer
• SET PTR TO ADDRESS OF X
• SET PTR1 TO PTR2
• The interesting point is that Cobol now
allows pointers to reference storage areas
that are inside the program.
• In older versions, Linkage areas could only
reference areas outside the program.
“Dropping a Linkage Area”
• To position a linkage section item onto a
storage area, use SET ADDRESS
Linkage Section.
01 X PIC X(8).
SET ADDRESS OF X TO PTR
Cobol Pointers
• This fundamentally changes how Cobol
can be written
• Data structures can now be supported in
Cobol (stacks, queues)
• Segmented records can be supported
Exercise #19
• Try running programs LINKED and
LINKED1 in BCST.SICCC01.PDSLIB
VSAM File Processing
128 32 8 2 1
64 16 4
128+64+32+0+8+0+ 0+1 = 233
Binary to Hex
• Conversion rule: Remove blocks of 4
binary digits and replace them with a
single hex digit
• 1101 1100 0011 1011
D C 3 B
• Hex dumps are made of hex digits and
represent binary values that are stored in
memory – a short-hand notation
• 2 HEX DIGITS = 1 BYTE
EBCDIC Characters
CHAR HEX CHAR HEX CHAR HEX CHAR HEX
0 = F0 A = C1 J = D1
1 = F1 B = C2 K = D2 S = E2
2 = F2 C = C3 L = D3 T = E3
3 = F3 D = C4 M = D4 U = E4
4 = F4 E = C5 N = D5 V = E5
5 = F5 F = C6 O = D6 W = E6
6 = F6 G = C7 P = D7 X = E7
7 = F7 H = C8 Q = D8 Y = E8
8 = F8 I = C9 R = D9 Z = E9
9 = F9
• Example: 110011
Changing: 001100
Add 1: 001100 + 1 = 001101 = 13
110011 = -13
Display
• The answer to all debugging problems is
to gain more information. DISPLAY can
provide it.
Debugging Lines
• It is possible to add debugging lines in
your code that can be logically removed by
commenting one line:
• Add a “WITH DEBUGGING MODE”
Phrase to your program:
ENVIRONMENT DIVISION.
SOURCE COMPUTER. IBM-XXX WITH DEBUGGING MODE.
Debugging Lines
• Put a D in column 7 on each debugging
line:
D IF X NOT = Y
D DISPLAY X
D END-IF