1 / 34

The Data Division

The Data Division. Main Two Sections. File Section Used to define files and record formats Field names within records Working Storage Section All accumulators, counters, subscripts, flags, tables, etc.

latham
Download Presentation

The Data Division

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Data Division

  2. Main Two Sections • File Section • Used to define files and record formats • Field names within records • Working Storage Section • All accumulators, counters, subscripts, flags, tables, etc. • All constant records, such as print header records and other constant or near-constant records. • But first:

  3. User-Defined Data Name Formation 1. 1 TO 30 CHARACTERS; LETTERS, DIGITS, HYPHENS 2. MUST NOT BEGIN (OR END) WITH A HYPHEN 3. NO EMBEDDED BLANKS 4. MUST CONTAIN AT LEAST ONE ALPHABETIC CHARACTER 5. CAN'T BE A COBOL RESERVED WORD. E.G., CAN'T SAY "DATA" AS A DATA-NAME (FIELD NAME) BUT CAN SAY "DATA-IN" OR "IN-DATA" , ETC. A TRADITION IS TO ADD THE SUFFIX -IN OR -OUT TO INPUT AND OUTPUT FIELDS. ALSO: -WK AND -S (SORT) ETC. AND OTHERS, AS IN TOTAL-WK... BETTER APPROACH: USE FIRST TWO CHARACTERS OF FILE NAME / RECORD NAME AS “PREFIXES.” ` E.G.: 01 EMPLOYEE-REC. 05 ER-NAME ... 05 ER-ADDRESS... 05 ER-PHONE WHY BETTER?? MUST CONFORM TO A CORPORATION’S “STANDARDS!” ONCE A FILE, RECORD, OR FIELD IS NAMED, THAT EXACT SAME NAME MUST BE USED THROUGHOUT THE PROGRAM UNIT TO REFER TO THAT ITEM. E.G., IF YOU USE "SSAN" - CAN'T USE "SN" OR "SSN" TO REFER TO THE SAME ITEM! (syntax error)

  4. Sections FILE SECTION INPUT AND OUTPUT FILES DEFINED HERE.ALL FILES DEFINED HERE. WORKING-STORAGE SECTION TEMPORARY DATA ITEMS TABLES, FLAGS, COUNTERS, ACCUMULATORS PRINT LINE HEADERS / TRAILERS MOST COBOL PROGRAMS HAVE AT LEAST ONE INPUT AND ONE OUTPUT FILE. . CAN HAVE MANY!!CONSIDER OLD-MASTER, TRANSACTION-FILE, NEW-MASTER FILE SCENARIO: A PERSONNEL FILE (OLD MASTER FILE) WITH ADD, CHANGE, AND DELETE TRANSACTIONS “AGAINST IT” PRODUCES A NEW MASTER FILE. MAY OPTIONALLY PRODUCE ACCOMPANYING REPORTS: TRANSACTION REGISTER, ERROR LIST CHANGE LIST. ALL ARE FILES IN COBOL AND MUST EACH BE DESCRIBED.

  5. User-Defined Data Name Formation 1. 1 TO 30 CHARACTERS; LETTERS, DIGITS, HYPHENS 2. MUST NOT BEGIN (OR END) WITH A HYPHEN 3. NO EMBEDDED BLANKS 4. MUST CONTAIN AT LEAST ONE ALPHABETIC CHARACTER 5. CAN'T BE A COBOL RESERVED WORD. E.G., CAN'T SAY "DATA" AS A DATA-NAME (FIELD NAME) BUT CAN SAY "DATA-IN" OR "IN-DATA" , ETC. A TRADITION IS TO ADD THE SUFFIX -IN OR -OUT TO INPUT AND OUTPUT FIELDS. ALSO: -WK AND -S (SORT) ETC. AND OTHERS, AS IN TOTAL-WK... BETTER APPROACH: USE FIRST TWO CHARACTERS OF FILE NAME / RECORD NAME AS “PREFIXES.” ` E.G.: 01 EMPLOYEE-REC. 05 ER-NAME ... 05 ER-ADDRESS... 05 ER-PHONE WHY BETTER?? MUST CONFORM TO A CORPORATION’S “STANDARDS!” ONCE A FILE, RECORD, OR FIELD IS NAMED, THAT EXACT SAME NAME MUST BE USED THROUGHOUT THE PROGRAM UNIT TO REFER TO THAT ITEM. E.G., IF YOU USE "SSAN" - CAN'T USE "SN" OR "SSN" TO REFER TO THE SAME ITEM! (syntax error)

  6. System Flow Chart Old Master File Transaction File Personnel Edit/Update Program New Master Fle (updated) Transaction Register Change List Error List

  7. Given this System Flow Chart: FD ENTRIES. (FILE DESCRIPTIONS…) EACH FILE IN PROGRAM MUST BE NAMED IN A SELECT STATEMENT . SELECT STATEMENTS FOUND WITHIN: FILE-CONTROL PARAGRAPH INPUT-OUTPUT SECTION ENVIRONMENT DIVISION. EACH FILE NAMED IN FILE-CONTROL PARAGRAPH (ABOVE) MUST BE DESCRIBED IN THE FILE SECTION IN THE DATA DIVISION. Given: FILE-CONTROL. SELECT OLD-MSTR-FILE ASSIGN TO DISK1. SELECT NEW-MSTR-FILE ASSIGN TO DISK2. SELECT TRANS-FILE ASSIGN TO DISK3. SELECT TRANS-REGISTER-FILE ASSIGN TO PRINTER. SELECT ERROR-LIST-FILE ASSIGN TO PRINTER. SELECT CHANGE-LIST-FILE ASSIGN TO PRINTER. REQUIRES SIX FILE DESCRIPTIONS IN THE DATA DIVISION – ONE FOR EACH NAMED FILE:

  8. FD Clauses File Description (FD) entries - clause . all FD clauses are optional in COBOL 85. . Label records clause Label Record is omitted Label Records are Standard Label Record is Undefined if using the clause, then select one. Often may be very helpful and most organizations will likely have standard labels…. Label Records are the first and last records of a tape file and present file-id information about the file that follows. Labels on files on disk in VTOC…or FAT or other directory type of structure. can contain much other information: data last written to LRECL (logical record length) BLKSIZE DSN (data set name) Security OPR (office of primary responsibility) and more. In Cobol 85, not a required clause; defaults to "standard“; In Cobol 74, clause was required. . FD Employee-File Label Records Standard. (a b areas)

  9. FD Clauses Record Contains clause.... Record contains {integer-1} characters . This is the size of the logical record! . This is the record you get via a read or write. . e.g. Record contains 80 characters. Could say, Record 80. . Always optional Advantage: if you include the record contains clause in your FD entries, every read and write operation causes the computer to check the length of the record you are reading or writing. If an error, it will let you know. Also, if the record description doesn’t match the number of characters specified in the record contains clause, you will get a compilation “warning.” Highly desirable! Use this..... It may save you time in diagnosing run time errors.

  10. FD CLAUSES Block Contains clause... Block contains {integer-1} records . Blocking speeds up i/o operations dramatically - but, of course, there is a cost.... . Blocking increases efficiency of utilization of tapes and disks. . Costs memory space; buffer space. . Explain a tape with 6250 bpi - Note that this is very old technology!!!! one 80 character records requires .0128" Blocked at 100, ==> requires 1.28" per 100 logical . Records per physical record (block) on tape. IBG or IRG is fixed by hardware - .75" is normal. . Optional clause. . Most assuredly will use in an operational environment!!! .. There will likely be a “standard” here...

  11. Logical and Physical Reads (and Writes) Discuss: . When your program executes a read (or write) (Logical read) .. The “01" = “the process area” .. Buffers and physical reads .. Pointer to “active” record .. Double buffering .. Storage cost .. contention for cpu cycles... Recognize that there is a difference between a logical and a physical read (or write). Devices only deal with physical records (blocks / sectors, …). The logical part is in your program.

  12. Record Descriptions A RECORD IS A COLLECTION OF RELATED DATA FIELDS WITHIN A FILE. MOST FILES: RECORDS ARE OF SAME LENGTH AND SAME FORMAT. . CALLED FIXED-LENGTH RECORDS. . HAVE “VARIABLE LENGTH RECORDS” TOO. CAN HAVE RECORDS OF THE SAME SIZE BUT OF DIFFERENT FORMAT IN THE SAME FILE..... CAN HAVE A SALARY HISTORY RECORD ( OF, SAY, SIZE 80) AND A PERSONNEL RECORD (ALSO OF SIZE 80) IN THE SAME FILE. . WOULD SAY: FD <FILE NAME> .... .... . 01 MF-SALARY-HIST-REC. .... .... .... 01 MF-PERSONNEL-REC. .... .... .

  13. Record Descriptions A very common occurrence.... Discuss: when reading a record, how does a program “know” which record format was read? Similarly for output: . Print files normally have multiple output formats: .. Report headers. .. Column headers .. Detail records .. Summary records .. Trailer records (at bottom of page) ... . These are different record types (formats) belonging to the same file (print file) . Each type requires a separate 01 record description, as you would expect. . Will return to this when we discuss “Working Storage Section.”

  14. Record Formats For each named file (Select statement), we have an FD . For each FD, we have one record descriptions for each record format in the file. . Each record format (record “description”): Names the record (the 01 level) Names the data fields in order in the record Shows any dependencies (group / elementary items) . File name is specified at the “FD level." . Record name is specified at the "01 level." . Each FD (file definition) is followed by a description of the record formats..

  15. Record Formats Record formats are named and described "at the 01 level. . Record name = highest level of data - the 01 level. . Other levels (higher numbers: 02-49) A field is a group of consecutive storage positions reserved for a data item Example: Let name for record be: . 01 Employee-Record-In. 05 ER-name-in .... 05 ER-annual-salary-in .... 05 ER-job-description-in .... Note the name of the record Note the number and names of the individual fields Note the relative position of the fields in the record Note that there will be additional info for each field (Coming) Note 01 is in the A-margin, while all other higher-numbered (but subordinate) fields will fall in the B-margin. These are sometimes referred to as a-area and b-area. Note: all 05's are not subordinate to each other, but they do follow each other in the record. .. Related only by relative position in the record.

  16. Unused 1 2 3-17 18-23 24-26 27-30 31-45 46-80 01 EF-EMPLOYEE-RECORD. 05 ER-NAME. 10 ER-INITIAL1 PIC X. 10 ER-INITIAL2 PIC X. 10 ER-LAST-NAME PIC X(15). 05 ER-ANNUAL-SALARY PIC 9(6). 05 ER-JOB-DESCRIPTION. 10 ER-TITLE. 15 ER-LEVEL PIC 999. 15 ER-POSITION PIC X(4). 10 ER-DUTIES PIC X(15). 05 PIC X(35).

  17. Hierarchy of Level Numbers INDENT FOR SUCCESSIVE LEVELNUMBERS PIC CLAUSES - SIZE AND CLASSIFICATION. CLASS (A,X,9) A ===> ? X ===> WHAT? 9 ===> ? SIZE X(4) OR XXXX OR 9(6)V99. GROUP AND ELEMENTARY ITEMS - DEFINITIONS... ELEMENTARY ITEMS MUST HAVE PIC CLAUSES GROUP ITEM WILL NOT HAVE PIC CLAUSES. STANDARD NAMES EACH DESCRIPTION ENDS WITH A PERIOD. THE ENTIRE RECORD MUST BE ACCOUNTED FOR......

  18. Summary of Picture Specifications A numeric pic clause can only contain .. Valid numeric characters and a sign (later on the sign) An alphanumeric field can contain any characters (including numbers) in the language. An alphabetic field can contain letters of the alphabet plus the space. Numbers in PIC X fields cannot be used directly in computations. These numbers are characters and not numbers!!! Discuss: Usage is Display. “Other usages... Later” 01 - record name only 02-49 - fields within the record 01 is an a-area entry 02-49 are b-area entries. Review group and elementary items PIC clauses on elementary items only!!!

  19. Implied Decimal Implied decimal example: 05 field-1 PIC 999V99. . A five position numeric field (not used for printing) . Used to house a number to be used in computations. . Used for storage of ‘numeric’ data with implied decimals . Used primary for inputs and storage – . Can be used for outputs too, but not normally used for outputs that are printed. If printed, 999V99 may be reformatted as 999.99 (five position, numeric field; six position report-item field…. More later…) .. Inputting values such as 1234 into a 99V99 will cause the inputted number to be ultimately treated as 12.34. .. Note: this (999V99) is considered a numeric field. Printing out a field with PIC 999V99 will result in displaying four numbers only. Normally not done. Print? Use 999.99 or similar edited field.

  20. Types of Data Variable data So far, we've talked about files, which consist of records, which contain fields. These file and record descriptions cause primary memory (ram) to be reserved for the records as we read them into memory from, say, disk and build them in memory to output back to disk or to printer Called variable data (explain why) Each read causes new data to be brought into ram .. Data values are not known .. The data format is. Data values are read in or, perhaps, computed Constant data Oftentimes "non-variable" or constant data is needed. Constant data is data needed for processing but not a part of an input record (that is, it is not read in".) Example: Add 1 to counter. Constant variable Move “Record not found" to printline Constant variable

  21. Two Ways to Get Constant Data into Program 1. By using the constantwhere needed in the Procedure Division Add 1 to counter Multiply 1.5 by rate giving over-time-rate 2. By defining “Working-Storage" data items with "value" clauses 01 one PIC 9 Value 1. 01 ot-factor PIC 9v9 Value 1.5. In usage: Add one to counter Multiply ot-factor by rate giving over-time-rate Constant variable variable But note: once we have a “data name”, we “can” change its value. Could say: move 3 to one. Then one has a value of 3! Very very poor programming - but can be done. Data declared in the working storage section within the data division can have value clauses (ahead...) Data defined in the file section of the data division cannot have Value clauses (exceptions coming). But this does not make sense!!!!

  22. Constants – Three Kinds Kinds of constants: Numeric literals; Non-numeric literals; Figurative Constants Consider: Add 1 to counter This is an example of a numeric literal type of constant A numeric literal "is a constant used primarily for arithmetic operations." Rules: 1. 1-18 digits 2. +, -, Option on the left 3. Decimal point may be embedded or precede the number; not the last character. Can say: add 0.3 to amt add .3 to amt add 3.0 to amt add +3.14 to result Subtract 0.067 from sum Can not say: add 3. To amt Add $1000 to total (only numbers and sign allowed.) Add 1,000 to amt (no special characters) Note: for storage in files, we rarely store the decimal. We store the data as 999V999 data; that is, ‘implied decimal.’ In the constant, however, we must always include the decimal, if it is part of the number.

  23. Constants – Three Kinds Kinds of constants: numeric literals; non-numeric literals; figurative constants Non-numeric literals (alphanumeric literals) . Is a constant . Is text and cannot be used for arithmetic Rules: 1. Enclosed in double quotes 2. 1 - 160 characters (Check on this size…) 3. Includes any characters except quotes (Special rules for enclosing quotes: later) Examples "Code" "input" "abc**123 " "$1,000" Can say Move “ABC" to Outfield and this moves the characters A , B, and C to the field, Outfield.

  24. Constants – Three Kinds Kinds of constants: numeric literals; non-numeric literals; figurative constants Alphanumeric literals must be enclosed in quotation marks. If not, it is not a non-numeric literal (constant) . Cannot say: Move data to Outfield (data is reserved) . Can say: Move "data" to Outfield . Can say Add 123 to Amt . Cannot say add "123" to amt. (Assumes amt is numeric field) . Can contain any characters. Compiler does not check to see of any reserved words are contained within the quotes. It doesn't care. Compare: Move xyz to field2 Move "xyz" to field2 Move 789 to amt-field Move "789" to amt-field Discuss?

  25. Constants – Three Kinds Kinds of constants: numeric literals; non-numeric literals; figurative constants Two main examples: zeroes, zeros and spaces and space There are several others: high values and low values zeroes and spaces are all reserved words meaning all spaces or all zeroes. Examples: Move spaces to printrec; (clears print area) Move zeroes to amt-field (resets amt-field to 0) Move zeroes to counter. (clears (sets to zero) counter) (Could say: Move 0 to Counter too)

  26. Working-Storage Section Any data-field necessary for processing. Not directly part of an input or output record. Typically, these items found in working storage are . Constants Temporary variables containing temporary values . Counters . Flags used a lot for counters . Accumulators flags, accumulators, . Tables initializing variables . Others... and setting up Syntax: Working-Storage Section. tables and constants Col 8 . Has individual working storage items within it.Example: 01 ws-stored-items. 05 f-are-there-more-records pic xxx value “yes”. 05 c-input-record-count pic 99 value zeroes. 05 c-num-02-commissions pic 999 value 0. 05 c-num-03-commissions pic 999 value 0. 05 c-total-amt-commissions pic 9999 value 0. . Value clauses are used all the time in COBOL programming.

  27. Example with Counter Example with Counters. If amount-of-sales-in > 100.00 Multiply 0.03 by amount-of-sales-in giving amt-commission-out Add 1 to c-num-03-commissions Else Multiply 0.02 by amount-of-sales-in giving amt-commission-out Add 1 t0 c-num-02-commissions End-if At end of file, print out the total number of people who received 2% commission and total number of people receiving 3%. Note: how “if” statement terminated: . Period could end it as could a scope terminator. . Will use “scope terminators” later . Need to recognize both!

  28. Example with Accumulators and Temporary Holding Variables Given: FD Printfile ....... . 01 print-rec. Explain this 01 summary-rec. 05 filler pic x(10). 05 p-total-commissions pic 9999. 05 filler pic x(118). Say you wanted to accumulateall the commissions and merely print out a single total amount of commission dollars saved in addition to the former totals... If amount-of-sales-in > 100.00 Multiply 0.03 by amount-of-sales-in Giving amt-commission-out, a-commission Else Multiply 0.02 by amount-of-sales-in Giving amt-commission-out, a-commission End-if Add a-commission to a-tot-commission. (A-commission and a-tot-commission are data items - accumulators - in working storage.)

  29. Example with EOF Indicators We have used the data name "are-there-more-records' as an end of file indicator. We have "initialized" it to "yes" Every time a record is read from the file, an end-of-file marker can be "tested for" by the read statement. Read some-file at end (that is, if we tried to read and no more records available) Move "no" to are-there-more-records. Or Read some-file at end Move “no” to are-there-more-records end-read Which do you like? Why? Which is more “error prone” to use? Why? . The eof of a file is sensed by computer reading the file. . We used “are-there-more-records” in working storage to hold a “yes” or “no.” .. Field is not part of record .. But needed for processing. .. We can set / reset at will... We can set its value and we can test its value and make the program do certain things based upon that test.

  30. Example with Reads and Priming the Pump – One way to go… We can say: Read sales file at end move "no" to are-there-more-records. Perform 200-commission-rtn Until are-there-more-records = "no". Note: we tested it each iteration of 200-commission-rtn!! Assumes we had: Working-Storage Section. 01 are-there-more-records pic xxx value "yes". Last statement in 200 commission-rtn is 200-commission-trn. ... <rest of logic in paragraph> Read sales file At end Move "no" to are-there-more-records end-Read.

  31. Example with Reads and Priming the Pump – One way to go… Discuss: Priming the pump! Could also use "eof" as end of file E.G.: Working-Storage Section. 01 eof pic 9 value 0. ….. <down in procedure division:> Read infile at end move 1 to eof end-read Perform 200-process-rtn until eof = 1 ... <more logic here> <down in program:> 200-process-rtn. ... <logic to process record just read…> Read infile At end move 1 to eof. <attempt to read next record at end of paragraph. If Read is unsuccessful, eof flag will be ‘set.’> 220-next-para. ...

  32. Value Clauses for Working-Storage Entries – Why?? When a program is loaded into storage, it occupies an area some other program used. Pic 999. reserves a three-byte area for numbers. Pic 999 value 0. Reserves a three byte area also and sets it to 0. This is why we set the print-rec to spaces before we build it. First time, we want to ensure it is clear.... If we did not clear the area out, we might have: junk |name-out| junk |commission-out| junk| Consider: 05 w-no-02-commissions pic 99. Note: no initial value in working- storage) Now execute: add 1 to w-no-02-commissions. Bomb!!! Why? Discuss... Fields you are moving into other fields do not required prior clearing. But if you are adding to a field, you are assuming that there is numeric data to add to.

  33. Value Clauses for Working-Storage Entries - More… Can initialize at the group level: 01 w-counters value zeroes. 05 w-count1 pic 999. 05 w-count2 pic 9v99. Consistency in value clauses and picture clauses: Data in the value clauses must be the same type as picture clauses 05 w-total pic 999 value zeroes ok value 0 ok value spaces. No!! Why not?? 05 w-total pic 9v99 value 4.75. Okay Value 0. Value 6.275. “ Value 26.275. Value spaces. No! Note the value clause is the same as a procedure division statement: “Move” but better, actually. Why??? Are occasions where value clause is inefficient...Much later.

  34. Value Clauses for Working-Storage Entries - More… Alphanumeric literals: 01 w-text-codes. 05 code-1 pic xxx value "abc“. Value "123" Value abc Value spaces Value " ". Are all okay??? Continuation of non-numeric literals 01 column-hdr pic x(123) value " " - " ".

More Related