Class

advertisement
Any Questions?
Agenda
•
•
•
•
Please review your marks
Procedure Division.
Chapter 3 – Working with Files
Chapter 4 – Printing!
2
Picture Clause Review
• Define Variables
• Define how Variables are displayed
3
Editing Functions
Function
Character
Printing of a / as a separator
Printing of Decimal Point in an integer
Suppress Leading Zeros in an integer
Print a Dollar Sign in front of an integer
/
.
Z
$
Print a Comma in an integer
Printing of + or – signs
Printing of ‘Debit’ or ‘Credit’ symbols
Printing of Spaces as separators
,
+ or DB or CR
B
Printing of Zeros as separators
Print Leading asterix
0
*
4
Examples
Trans-Amount PIC 9(6)V999 Value 4565.78.
Report-Item
PIC 9(6)V999
PIC $999999V999
PIC $ZZZ,ZZZ.99
PIC $ZZZ,ZZZ.9
Edited Results
5
Examples
Trans-Amount PIC 9(6)V999 Value 4565.78.
Report-Item
PIC $ZZZ,ZZZ.99PIC $ZZZ,ZZZ.99CR
PIC $ZZZZZZB99
PIC $ZZZZZZ.99+
Edited Results
6
Procedure Division
• Program Logic
• Statements are executed in the order that
they appear.
• Logic can be divided into paragraphs (subroutines)
• Some advice: Make sure a logical paragraph
is terminated with a period at the end of the
last statement line in the paragraph.
The Procedure Division
• It contains all the instructions the program
will execute
• Any variable or data field referred by any
instruction must have been defined in the
DATA DIVISION
• The instructions are known as ‘statements’
8
Procedure Division
(Syntax)
PROCEDURE DIVISION.
paragraph-name.
statements.
Procedure Division Structure
PROCEDURE DIVISION.
00-MAIN.
OPEN INPUT PAYROLL-FILE
OUTPUT PRINT-FILE.
READ PAYROLL-FILE
AT END MOVE 'N' TO EOF-FLAG
END-READ.
PERFORM HEADER-LINE
PERFORM PROCESS-RECORDS
UNTIL EOF-FLAG = 'N'.
CLOSE PAYROLL-FILE
PRINT-FILE.
STOP RUN.
Paragraph.( Pos 8-….)
A sentence/statement starts at pos 12 till ….
EJECT
HEADER-LINE.
MOVE HEADING-LINE TO PRINT-LINE.
WRITE PRINT-LINE.
PROCESS-RECORDS.
MOVE EMP-NAME TO DET-NAME
MOVE EMP-HOURS TO DET-HOURS
MOVE DETAIL-LINE TO PRINT-LINE
COMPUTE DET-PAY = EMP-HOURS * EMP-RATE
WRITE PRINT-LINE
READ PAYROLL-FILE
AT END MOVE 'N' TO EOF-FLAG
END-READ.
Statement or
Instruction
Sentence or
group of
Statement
10
Coding a COBOL Program
• Column based
• 3 parts to a COBOL source line
– Continuation (-) (in col 7)
– Area A (col. 8)
– Area B (col 12)
Program Comments (*)
• * to start a comment line
• Generally placed in column 7 but could be
used anywhere at the end of a statement
preceded by a space. i.e. …–name. *
Area A (col 8)
• Division Names
• Section Names
• Paragraph Names
Area B
Everything else
(anywhere from col 12 to 72)
Frequently Used COBOL Verbs
Types of Statements
• Arithmetic: Add,
Subtract, Compute
• Input/Output: Read
file records
• Data Manipulation:
Move
• Selection:
If..Then..Else
• Iteration:
Perform…Until
• Case Structure:
Evaluate..When
• Selection, Iteration,
Case: Structured
Programming
Constructs
16
Working With Files
(Procedure Division)
(rest of Chapter 3)
17
Opening A File
OPEN STATEMENT
• A File must be open
before its records are
processed
• The OPEN process
makes the record
buffer available to the
program
• The buffer is named in
the File Section as the
DATA RECORD
• An OPEN will
determine how to read
or write in the file
18
Opening A File
OPEN STATEMENT
A Sequential File can be opened
in different modes:
• Input: Records are only read not modified
• Output: Records are new or
written over.
• I-O read and or update existing
records.
• Extend - New records to be
appended after the last record
• When open as output new
blank record space is
assigned and all fields
should be populated!
19
OPEN Statement
• Usually the first statement in a program
• Open files for processing
• Terminator is a period
OPEN STATEMENT
OPEN mode file-name
OPEN INPUT MASTER-FILE
OPEN OUTPUT NEW-PAYROLL
OPEN EXTEND NEW-PAYROLL
21
OPEN, CLOSE, and STOP RUN
STATEMENTS
PROCEDURE DIVISION.
00-MAIN.
OPEN INPUT PAYROLL-FILE
OUTPUT PRINT-FILE.
=========
CLOSE PAYROLL-FILE
PRINT-FILE.
STOP RUN.
Each file gets an
OPEN.
Each OPEN has
a CLOSE: File
has to be closed
before to end
the program
22
CLOSE Statement
• Closes the files used in the program
• Usually the second to last set of statements
executed in the program
• Terminator is .
STOP RUN Statement
• Terminates the program
• The last statement executed in the program.
• Terminator is .
OPEN Statement
• Usually the first statement in a program
• Open files for processing
• Terminator is a period
25
READ Statement
•
•
•
•
Used to Read a Record from a File
End of File Logic
Not End of File Logic
Terminator - END-READ.
26
READ Statement
READ Employee-File
AT END
PERFORM 500-End-Of-File-Routine
NOT AT END
PERFORM 300-Process-Employee
END-READ.
27
WRITE Statement
• Writes/Updates records in a file
• Terminator is .
28
CLOSE Statement
• Closes the files used in the program
• Usually the second to last set of statements
executed in the program
• Terminator is .
29
STOP RUN Statement
• Terminates the program
• The last statement executed in the program.
• Terminator is .
30
Printing on the AS/400
Job
Output Queue
Program/
Command
Spooled File
Data
*FILE
Report
Layout
(Printer
File)
*FILE
31
A Good Report…
• Heading (a meaningful report name)
– Date and Page Number
• Column Headings (to identify data)
• Order column from left to right to highlight
significant data
• Edit numbers for readability
• Include Totals at end of report and groups of items
• ** To identify the ‘level’ of a total
• Clearly indicate the end of the report.
32
Record Formats
• Each Record format is defined using it’s
own Group-Item in the Working Storage
Section.
• Record format name is defined using the 01
level
• Fields and literals are defined using
subsequent levels with Picture & Values
Clauses
33
Customer Accounts
Customer Accounts
Date: 05/04/00
Date
Customer: 1015 Cindy Laurin-Moogk
05/01/00
05/02/00
05/03/00
Amount
* Totals: 1015 Cindy Laurin-Moogk
$600.00
Page: 999
$100.00
$200.00
$300.00
Customer: 1016 Bruce Nugent
05/01/00
$100.00
* Totals: 1016 Bruce Nugent
$100.00
** Totals: All Customers
*** End of Report
$700.00
34
How Many Record Formats?
Heading
Customer Accounts
Date: 05/04/00
Page: 999
Column Heading
Customer Heading
Detail Line
Detail Line
Detail Line
Date
Customer: 1015 Cindy Laurin-Moogk
05/01/00
05/02/00
05/03/00
Amount
Customer Total
* Totals: 1015 Cindy Laurin-Moogk
$600.00
Customer Heading
Detail Line
Customer: 1016 Bruce Nugent
Customer Total
* Totals: 1016 Bruce Nugent
$100.00
Report Total
End of Report
** Totals: All Customers
*** End of Report
$700.00
05/01/00
$100.00
$200.00
$300.00
$100.00
35
Page Number
• Manually Calculated
• Need to know how many lines print on a
page
– Printer File
• Your programs must have:
– Line-Counter Variable
– Page-Counter Variable
36
Printer Spacing Charts
Report
Format
Line
Number
2 . . . . . .
1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 1 . . ..
37
REMEMBER
to
READ a file
But
WRITE a RECORD
Sequential READ Statement
•
•
•
•
Used to Read the next Record from a File
End of File Logic
Not End of File Logic
Terminator - END-READ.
Sequential READ Statement
READ Employee-File
AT END
PERFORM 500-End-Of-File-Routine
NOT AT END
PERFORM 300-Process-Employee
END-READ.
READ STATEMENT
PROCEDURE DIVISION.
00-MAIN.
OPEN INPUT PAYROLL-FILE
OUTPUT PRINT-FILE.
READ PAYROLL-FILE
AT END MOVE 'N' TO EOF-FLAG
END-READ.
01 EMPLOYEE-IN.
05 EMP-NAME
05 EMP-HOURS
05 EMP-RATE
05 EMP-DEP
PIC X(25).
PIC 9(03).
PIC 99V9 USAGE COMP-3.
PIC X(15).
41
READing A FILE
• The READ statement ‘gets’ the next record
in the file
• Format:
READ file-name AT END statement.
• After execution the record is moved to the
buffer declared in the file section as ‘recordname’
42
Display File Read
• Used to read information from a screen
• RECORD keyword is needed because
display files usually have more than one
record format.
Read Display-file RECORD.
WRITE Statement
• Writes/Outputs records to a file
• Terminator is . Or END-WRITE.
• ‘Format is’ clause needed for display files
• Format is record format
• Needed because display files usually
have more than one record format
(screen)
The WRITE Statement
• It writes a record to a file
• An OPEN OUTPUT or OPEN EXTEND
has been issued prior to the WRITE
• WRITE adds a record to a data file
• WRITE prints a record to a printer file
• Sequential Write:
WRITE file-record-name
45
Writing to a Physical File
Write Product-record
invalid key
display ‘write failed’
not invalid key
display ‘write ok’
End-write.
Writing to a Report
Write print-record-out.
Writing to a Display File
Write display-record
Format is ‘RECORD1’
end-write.
Changing Data - REWRITE
• Updates a record in a file
• File must be opened as I-O instead of input
• Record must be read first before rewrite
REWRITE record-name (FROM variable-name)
INVALID KEY perform error-rtn
NOT INVALID KEY perform continue-rtn
END-REWRITE.
Examples
Customer Name PIC X(20) Value ‘Cindy
Laurin-Moogk’
Report Item
PIC X(20)
PIX XXXXXXXXXXBXX
50
Customer Accounts
1
2
3
4
5
12345678901234567890123456789012345678901234567890123456789
Customer Accounts
Date: 05/04/00 Page: 999
Date
Amount
Customer: 1015 Cindy Laurin-Moogk
05/01/00
$100.00
05/02/00
$200.00
05/03/00
$300.00
*
Totals: 1015 Cindy Laurin-Moogk
$600.00
Customer: 1016 Bruce Nugent
05/01/00
$100.00
*
$100.00
Totals: 1016 Bruce Nugent
** Totals: All Customers
*** End of Report
$700.00
51
Printing in COBOL
WORKING STORAGE.
FILE SECTION
FD Customer-Report.
01 Print-Record-Out.
01 Heading-1
05
PIC X(8) Value Spaces.
05
PIC X(17) Value ‘Customer
Accounts’
05
PIC X(5) Value Spaces.
05
PIC X(6) Value ‘Date: ‘
05 WS-Date-Out PIC 99/99/99.
05
PIC X(1) Value Spaces.
05
PIC X(6) Value ‘Page: ‘.
05 WS-Page PIC 9(3).
52
Printing or Writing to a Spooled File
(using AFTER ADVANCING)
Detail-Line-1 PIC X(80)
VALUE ‘Cindy’s Test’.
Detail-Line-2 PIC X(80)
VALUE ‘COBOL Line of Code’.
WRITE Print-Record-Out FROM Detail-Line-1.
WRITE Print-Record-Out FROM Detail-Line-2
AFTER ADVANCING 1 LINE.
53
Printing or Writing to a Spooled File
(using BEFORE ADVANCING)
Detail-Line-1 PIC X(80)
VALUE ‘Cindy’s Test’.
Detail-Line-2 PIC X(80)
VALUE ‘COBOL Line of Code’.
WRITE Print-Record-Out FROM Detail-Line-1
BEFORE ADVANCING 1 LINE.
WRITE Print-Record-Out FROM Detail-Line-2.
54
Printing or Writing to a Spooled File
Detail-Line-1 PIC X(80)
VALUE ‘Cindy’s Test’.
Detail-Line-2 PIC X(80)
VALUE ‘COBOL Line of Code’.
WRITE Print-Record-Out FROM Detail-Line-1
AFTER ADVANCING 1 LINE.
WRITE Print-Record-Out FROM Detail-Line-2
BEFORE ADVANCING 1 LINE.
55
Sorting Data
Using Access Paths
Physical Files vs Logical Files
EMPLOYEEPF *FILE
EMPLOYEE *FILE
Physical Files or Logical Files?
Externally Described Files
• Select Statement when physical/logical file
has a key.
SELECT Cobol-file-name
ASSIGN TO database-actual-file-name
[ORGANIZATION IS INDEXED]
[ACCESS MODE IS SEQUENTIAL]
RECORD KEY is data-element.
(data-element could be EXTERNaLLY-DESCRIBED-KEY)
Externally Described Files
• Copying the record layout.
FD Cobol-file-name.
01 Cobol-Record-Name.
COPY DD-actualrecordname OF actualfilename.
(DD can be replaced by DDS if you require the 10
char field names instead of the aliases)
Handy Physical File Commands
DSPPFM – Display Physical File Member
Displays the contents of a Physical File in arrival
sequence.
DSPFD – Display File Description
Information about the file – eg access path.
DSPFFD – Display File Field Description
Displays the fields in the file.
Random Reads
• Used to retrieve a record based on the value
of a key field
• Need an access path sorted by the key field
needed
• Select statement changes
Defining a Random Access File
SELECT Employee-File
ASSIGN to DATABASE-EMPPF
ORGANIZATION is INDEXED
ACCESS MODE is RANDOM
RECORD KEY is
EXTERNALLY-DESCRIBED-KEY
(with duplicates).
Random Reads
• If the key field to EMPPF is the EmployeeNumber then:
Move 1 to Employee-Number.
Read Employee-File
Invalid Key
Move ‘Error’ to Employee-Name-Out
Not invalid key
Move Employee-Name to Employee-Nameout
End-Read.
Validating the Province Code
Database object: PROVINCES
Problem
FILENAME is a 10 character field in the file,
FILELIST.
FILELIST is a Physical file that contains a record for
each of the physical files in QGPL.
You are required to write a COBOL program that
prints all of the files listed in FILELIST.
What COBOL statement would list all of the Files in
QGPL that start with ‘CHAP’?
65
ACCEPT Statement
ACCEPT variable FROM
DAY-OF-WEEK
DATE
DAY
TIME
DAY-OF-WEEK = 1-7 (Mon-Sun)
DATE = YYMMDD
DAY = Julian Date
TIME = hhmmsshh
66
Convert YYMMDD to
MMDDYY?
01 Heading-2.
05 HL-RUN-MNTH
05 HL-RUN-DAY
05 HL-RUN-YEAR
01 WS-RUN-DATE
05 WS-RUN-YEAR
05 WS-RUN-MONTH
05 WS-RUN-DAY
ACCEPT WS-RUN-DATE FROM DATE.
MOVE WS-RUN-MONTH TO HL-RUN-MONTH.
MOVE WS-RUN-DAY
TO HL-RUN-DAY.
MOVE WS-RUN-YEAR
TO HL-RUN-YEAR.
67
Indicators in Display Files
Option Indicators in COBOL
• Binary Values
• Working Storage Definition example
01 ws-indicator-list.
05 IN88 INDICATOR 88 PIC 1 value B’0’.
• Passed to display files in the write statement
Write Display-record
format is ‘RECORD1’
indicators are ws-indicator-list.
Example – Indicator 90 displays the
message, Employee Name is invalid
Working Storage.
01 ws-indicators.
05 IN90 INDICATOR 90 pic 1.
Procedure Division.
If scr-employee-name is equal to spaces
move b’1’ to IN90
else
move b’0’ to IN90
end-if.
Response Indicators in COBOL
• Stored as binary values in the display file and
passed as PIC X(2) via the control Area.
Select Display-file
assign to workstation-dspfile-si
organization is transaction
control area is ws-control.
Working Storage Section.
01 ws-control.
05 ws-function-key pic x(2).
Example – check to see if function
key 3 was pressed.
Select Display-file
assign to workstation-dspfile-si
organization is transaction
control area is ws-control.
Working Storage Section.
01 ws-control.
05 ws-function-key pic x(2).
Procedure Division.
if ws-function-key = ’03’
…..
end-if.
Download