Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
COBOL
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Data division=== The data division is split into six sections which declare different items: the file section, for file records; the working-storage section, for [[static variable]]s; the local-storage section, for [[automatic variable]]s; the linkage section, for parameters and the return value; the report section and the screen section, for [[text-based user interface]]s. ====Aggregated data==== Data items in COBOL are declared hierarchically through the use of level-numbers which indicate if a data item is part of another. An item with a higher level-number is subordinate to an item with a lower one. Top-level data items, with a level-number of 1, are called ''{{dfn|records}}''. Items that have subordinate aggregate data are called ''{{dfn|group items}}''; those that do not are called ''{{dfn|elementary items}}''. Level-numbers used to describe standard data items are between 1 and 49.{{sfn|ISO/IEC JTC 1/SC 22/WG 4|2014|loc=§ 8.5.1.2}}{{sfn|Cutler|2014|loc=Appendix A}} <syntaxhighlight lang="cobol"> 01 some-record. *> Aggregate group record item 05 num PIC 9(10). *> Elementary item 05 the-date. *> Aggregate (sub)group record item 10 the-year PIC 9(4). *> Elementary item 10 the-month PIC 99. *> Elementary item 10 the-day PIC 99. *> Elementary item </syntaxhighlight> In the above example, elementary item {{code|num}} and group item {{code|the-date}} are subordinate to the record {{code|some-record}}, while elementary items {{code|the-year}}, {{code|the-month}}, and {{code|the-day}} are part of the group item {{code|the-date}}. Subordinate items can be disambiguated with the {{code|IN}} (or {{code|OF}}) keyword. For example, consider the example code above along with the following example: <syntaxhighlight lang="cobol"> 01 sale-date. 05 the-year PIC 9(4). 05 the-month PIC 99. 05 the-day PIC 99. </syntaxhighlight> The names {{code|the-year}}, {{code|the-month}}, and {{code|the-day}} are ambiguous by themselves, since more than one data item is defined with those names. To specify a particular data item, for instance one of the items contained within the {{code|sale-date}} group, the programmer would use {{code|the-year IN sale-date}} (or the equivalent {{code|the-year OF sale-date}}). This syntax is similar to the "dot notation" supported by most contemporary languages. ====Other data levels==== A level-number of 66 is used to declare a re-grouping of previously defined items, irrespective of how those items are structured. This data level, also referred to by the associated {{dfn|{{code|RENAMES}} clause}}, is rarely used<ref>{{cite book | title=Sams Teach Yourself COBOL in 24 hours | publisher=[[Sams|SAMS Publishing]] | year=1999 | pages=40 | isbn=978-0672314537 | first=Thane | last=Hubbell | lccn=98087215}}</ref> and, circa 1988, was usually found in old programs. Its ability to ignore the hierarchical and logical structure data meant its use was not recommended and many installations forbade its use.{{sfn|McCracken|Golden|1988|loc=§ 19.9}} <syntaxhighlight lang="cobol"> 01 customer-record. 05 cust-key PIC X(10). 05 cust-name. 10 cust-first-name PIC X(30). 10 cust-last-name PIC X(30). 05 cust-dob PIC 9(8). 05 cust-balance PIC 9(7)V99. 66 cust-personal-details RENAMES cust-name THRU cust-dob. 66 cust-all-details RENAMES cust-name THRU cust-balance. </syntaxhighlight> A 77 level-number indicates the item is stand-alone, and in such situations is equivalent to the level-number 01. For example, the following code declares two 77-level data items, {{code|property-name}} and {{code|sales-region}}, which are non-group data items that are independent of (not subordinate to) any other data items: <syntaxhighlight lang="cobol"> 77 property-name PIC X(80). 77 sales-region PIC 9(5). </syntaxhighlight> An 88 level-number declares a ''{{dfn|condition name}}'' (a so-called 88-level) which is true when its parent data item contains one of the values specified in its {{code|VALUE}} clause.{{sfn|Cutler|2014|loc=§ 5.8.5}} For example, the following code defines two 88-level condition-name items that are true or false depending on the current character data value of the {{code|wage-type}} data item. When the data item contains a value of {{code|'H'}}, the condition-name {{code|wage-is-hourly}} is true, whereas when it contains a value of {{code|'S'}} or {{code|'Y'}}, the condition-name {{code|wage-is-yearly}} is true. If the data item contains some other value, both of the condition-names are false. <syntaxhighlight lang="cobol"> 01 wage-type PIC X. 88 wage-is-hourly VALUE "H". 88 wage-is-yearly VALUE "S", "Y". </syntaxhighlight> ====Data types==== Standard COBOL provides the following data types:{{sfn|ISO/IEC JTC 1/SC 22/WG 4|2014|loc=§ 8.5.2}} {| class="wikitable" |- ! Data type ! Sample declaration ! Notes |- | Alphabetic | {{code|PIC A(30)|lang=cobolfree}} | May contain only letters or spaces. |- | Alphanumeric | {{code|PIC X(30)|lang=cobolfree}} | May contain any characters. |- | Boolean | {{code|PIC 1 USAGE BIT|lang=cobolfree}} | Data stored in the form of 0s and 1s, as a binary number. |- | Index | {{code|USAGE INDEX|lang=cobolfree}} | Used to reference table elements. |- | National | {{code|PIC N(30)|lang=cobolfree}} | Similar to alphanumeric, but using an extended character set, e.g. [[UTF-8]]. |- | Numeric | {{code|PIC 9(5)V9(2)|lang=cobolfree}} | Contains exactly 7 digits (7=5+2). 'V' locates the implicit decimal in a fixed point number. |- | Object | {{code|USAGE OBJECT REFERENCE|lang=cobolfree}} | May reference either an object or <code>NULL</code>. |- | Pointer | {{code|USAGE POINTER|lang=cobolfree}} | |} Type safety is variable in COBOL. Numeric data is converted between different representations and sizes silently and alphanumeric data can be placed in any data item that can be stored as a string, including numeric and group data.{{sfn|ISO/IEC JTC 1/SC 22/WG 4|2014|loc=§ 14.9.24}} In contrast, object references and pointers may only be assigned from items of the same type and their values may be restricted to a certain type.{{sfn|ISO/IEC JTC 1/SC 22/WG 4|2014|loc=§ 14.9.35}} =====PICTURE clause===== A {{code|PICTURE}} (or {{code|PIC}}) clause is a string of characters, each of which represents a portion of the data item and what it may contain. Some picture characters specify the type of the item and how many characters or digits it occupies in memory. For example, a {{code|9}} indicates a decimal digit, and an {{code|S}} indicates that the item is [[signedness|signed]]. Other picture characters (called ''{{dfn|insertion}}'' and ''{{dfn|editing}}'' characters) specify how an item should be formatted. For example, a series of {{code|+}} characters define character positions as well as how a leading sign character is to be positioned within the final character data; the rightmost non-numeric character will contain the item's sign, while other character positions corresponding to a {{code|+}} to the left of this position will contain a space. Repeated characters can be specified more concisely by specifying a number in parentheses after a picture character; for example, {{code|9(7)}} is equivalent to {{code|9999999}}. Picture specifications containing only digit ({{code|9}}) and sign ({{code|S}}) characters define purely ''{{dfn|numeric}}'' data items, while picture specifications containing alphabetic ({{code|A}}) or alphanumeric ({{code|X}}) characters define ''{{dfn|alphanumeric}}'' data items. The presence of other formatting characters define ''{{dfn|edited numeric}}'' or ''{{dfn|edited alphanumeric}}'' data items.{{sfn|ISO/IEC JTC 1/SC 22/WG 4|2014|loc=§ 13.18.40}} {| class="wikitable" |+ Examples |- ! {{code|PICTURE}} clause ! Value in ! Value out |- | rowspan="2" | {{code|PIC 9(5)|lang=cobolfree}} | {{code|100}} | {{code|00100}} |- | {{code|"Hello"}} | {{code|"Hello"}} (this is legal, but results in [[undefined behavior]]){{sfn|ISO/IEC JTC 1/SC 22/WG 4|2014|loc=§ 14.9.24}} |- | {{code|PIC +++++|lang=cobolfree}} | {{code|-10}} | <code>" -10"</code><!-- {{code| -10}} deletes one of the spaces --> (note leading spaces) |- | {{code|PIC 99/99/9(4)|lang=cobolfree}} | {{code|30042003}} | {{code|"30/04/2003"}} |- | rowspan="2" | {{code|PIC *(4)9.99|lang=cobolfree}} | {{code|100.50}} | {{code|"**100.50"}} |- | <code>0</code> <!-- {{code|0}} produces a space for some reason --> | {{code|"****0.00"}} |- | {{code|PIC X(3)BX(3)BX(3)|lang=cobolfree}} | {{code|"ABCDEFGHI"}} | {{code|"ABC DEF GHI"}} |} =====USAGE clause===== {{missing information|section|COMPUTATIONAL-5|date=April 2021}} The {{code|USAGE}} clause declares the format in which data is stored. Depending on the data type, it can either complement or be used instead of a {{code|PICTURE}} clause. While it can be used to declare pointers and object references, it is mostly geared towards specifying numeric types. These numeric formats are:{{sfn|ISO/IEC JTC 1/SC 22/WG 4|2014|loc=§ 13.18.60.3}} * Binary, where a minimum size is either specified by the <code>PICTURE</code> clause or by a <code>USAGE</code> clause such as <code>BINARY-LONG</code> * {{code|USAGE COMPUTATIONAL|lang=cobolfree}}, where data may be stored in whatever format the implementation provides; often equivalent to {{code|USAGE BINARY|lang=cobolfree}} * {{code|USAGE DISPLAY|lang=cobolfree}}, the default format, where data is stored as a string * Floating-point, in either an implementation-dependent format or according to IEEE 754 * {{code|USAGE NATIONAL|lang=cobolfree}}, where data is stored as a string using an extended character set * {{code|USAGE PACKED-DECIMAL|lang=cobolfree}}, where data is stored in the smallest possible decimal format (typically [[packed binary-coded decimal]]) ====Report writer==== The report writer is a [[declarative programming|declarative facility]] for creating reports. The programmer need only specify the report layout and the data required to produce it, freeing them from having to write code to handle things like page breaks, data formatting, and headings and footings.{{sfn|ISO/IEC JTC 1/SC 22/WG 4|2014|p=855}} Reports are associated with report files, which are files which may only be written to through report writer statements. <syntaxhighlight lang="cobol"> FD report-out REPORT sales-report. </syntaxhighlight> Each report is defined in the report section of the data division. A report is split into report groups which define the report's headings, footings and details. Reports work around hierarchical ''{{dfn|control breaks}}''. Control breaks occur when a key variable changes it value; for example, when creating a report detailing customers' orders, a control break could occur when the program reaches a different customer's orders. Here is an example report description for a report which gives a salesperson's sales and which warns of any invalid records: <syntaxhighlight lang="cobol"> RD sales-report PAGE LIMITS 60 LINES FIRST DETAIL 3 CONTROLS seller-name. 01 TYPE PAGE HEADING. 03 COL 1 VALUE "Sales Report". 03 COL 74 VALUE "Page". 03 COL 79 PIC Z9 SOURCE PAGE-COUNTER. 01 sales-on-day TYPE DETAIL, LINE + 1. 03 COL 3 VALUE "Sales on". 03 COL 12 PIC 99/99/9999 SOURCE sales-date. 03 COL 21 VALUE "were". 03 COL 26 PIC $$$$9.99 SOURCE sales-amount. 01 invalid-sales TYPE DETAIL, LINE + 1. 03 COL 3 VALUE "INVALID RECORD:". 03 COL 19 PIC X(34) SOURCE sales-record. 01 TYPE CONTROL HEADING seller-name, LINE + 2. 03 COL 1 VALUE "Seller:". 03 COL 9 PIC X(30) SOURCE seller-name. </syntaxhighlight> The above report description describes the following layout: <pre> Sales Report Page 1 Seller: Howard Bromberg Sales on 10/12/2008 were $1000.00 Sales on 12/12/2008 were $0.00 Sales on 13/12/2008 were $31.47 INVALID RECORD: Howard Bromberg XXXXYY Seller: Howard Discount ... Sales Report Page 12 Sales on 08/05/2014 were $543.98 INVALID RECORD: William Selden 12052014FOOFOO Sales on 30/05/2014 were $0.00 </pre> Four statements control the report writer: {{code|INITIATE}}, which prepares the report writer for printing; {{code|GENERATE}}, which prints a report group; {{code|SUPPRESS}}, which suppresses the printing of a report group; and {{code|TERMINATE}}, which terminates report processing. For the above sales report example, the procedure division might look like this: <syntaxhighlight lang="cobol"> OPEN INPUT sales, OUTPUT report-out INITIATE sales-report PERFORM UNTIL 1 <> 1 READ sales AT END EXIT PERFORM END-READ VALIDATE sales-record IF valid-record GENERATE sales-on-day ELSE GENERATE invalid-sales END-IF END-PERFORM TERMINATE sales-report CLOSE sales, report-out . </syntaxhighlight> Use of the Report Writer facility tends to vary considerably; some organizations use it extensively and some not at all.{{sfn|McCracken|1976|p=338}} In addition, implementations of Report Writer ranged in quality, with those at the lower end sometimes using excessive amounts of memory at runtime.{{sfn|McCracken|1976|p=338}}
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
COBOL
(section)
Add topic