Any Questions?
Agenda Please review your marks Procedure Division. Chapter 3 – Working with Files Chapter 4 – Printing!
Picture Clause Review Define Variables Define how Variables are displayed
Editing Functions Function Character Printing of a / as a separator / Printing of Decimal Point in an integer . Suppress Leading Zeros in an integer Z Print a Dollar Sign in front of an integer $ Print a Comma in an integer , Printing of + or – signs + or - Printing of ‘Debit’ or ‘Credit’ symbols DB or CR Printing of Spaces as separators B Printing of Zeros as separators Print Leading asterix *
Examples Trans-Amount PIC 9(6)V999 Value 4565.78. Report-Item Edited Results PIC 9(6)V999 PIC $999999V999 PIC $ZZZ,ZZZ.99 PIC $ZZZ,ZZZ.9
Examples Trans-Amount PIC 9(6)V999 Value 4565.78. Report-Item Edited Results PIC $ZZZ,ZZZ.99- PIC $ZZZ,ZZZ.99CR PIC $ZZZZZZB99 PIC $ZZZZZZ.99+
Procedure Division Program Logic Statements are executed in the order that they appear. Logic can be divided into paragraphs (sub-routines) 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’
Procedure Division (Syntax) paragraph-name. statements.
Procedure Division Structure 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. 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 Paragraph.( Pos 8-….) A sentence/statement starts at pos 12 till …. Statement or Instruction Sentence or group of Statement
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
Working With Files (Procedure Division) (rest of Chapter 3)
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
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!
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
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
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
READ Statement Used to Read a Record from a File End of File Logic Not End of File Logic Terminator - END-READ.
READ Statement READ Employee-File AT END PERFORM 500-End-Of-File-Routine NOT AT END PERFORM 300-Process-Employee END-READ.
WRITE Statement Writes/Updates records in a file Terminator is .
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 .
Printing on the AS/400 Job Output Queue Program/ Command Spooled File Data Report Layout (Printer File) *FILE *FILE
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.
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
Customer Accounts 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 * Totals: 1016 Bruce Nugent $100.00 ** Totals: All Customers $700.00 *** End of Report
How Many Record Formats? Heading Customer Accounts Date: 05/04/00 Page: 999 Column Heading Date Amount Customer Heading Customer: 1015 Cindy Laurin-Moogk Detail Line 05/01/00 $100.00 Detail Line 05/02/00 $200.00 Detail Line 05/03/00 $300.00 Customer Total * Totals: 1015 Cindy Laurin-Moogk $600.00 Customer Heading Customer: 1016 Bruce Nugent Customer Total * Totals: 1016 Bruce Nugent $100.00 Report Total ** Totals: All Customers $700.00 End of Report *** End of Report
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
Printer Spacing Charts Report Format Line Number 1 2 3 4 5 6 7 8 9 2 . . . . . . . . ..
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 PIC X(25). 05 EMP-HOURS PIC 9(03). 05 EMP-RATE PIC 99V9 USAGE COMP-3. 05 EMP-DEP PIC X(15).
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 ‘record-name’
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
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
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 * Totals: 1016 Bruce Nugent $100.00 ** Totals: All Customers $700.00 *** End of Report
Printing in COBOL FILE SECTION FD Customer-Report. WORKING STORAGE. 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). FILE SECTION FD Customer-Report. 01 Print-Record-Out.
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.
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.
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.
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 Employee-Number 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-Name- out 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’?
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
Convert YYMMDD to MMDDYY? 01 Heading-2. 01 WS-RUN-DATE 05 HL-RUN-MNTH 05 WS-RUN-YEAR 05 HL-RUN-DAY 05 WS-RUN-MONTH 05 HL-RUN-YEAR 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.
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.