cpan320-lec6

advertisement
CPAN 320 COBOL
Lecture 6
Editing and producing printed output
Editing functions:
When you process input data and produce a printed report you would like it to
be clearer, neater and easy to read by the users. Editing symbols are used for
this purpose. Editing symbols may be used with report items, which may be
defined as part of the output record or in a WORKING-STORAGE area that
will be moved to the output record.
The Editing function is accomplished by moving data from an elementary
sending field to an elementary receiving field that has edit symbol in its PIC
clause. Report items can’t be used in arithmetic operation other than as a
receiving field.
We will discuss the following seven Editing functions, the first six editing
functions may be used with numeric fields only, and the last one can be used
with any type of field.
1. Printing (or displaying) decimal points where decimal alignment is
implied.
A decimal point (.) is used in the report item PIC clause when it
receives data from sending field with implied decimal point.
For example if we have an elementary item defined as
AMNT-IN
PIC 99V9
The corresponding report item that will display a decimal point should
be
AMNT-OUT PIC 99.9
More examples: The first line of each filed specify the field name, type
and size where as the second line show the field content.
Sending Field
TAX-IN PIC V99
15
Report item receiving field
TAX-OUT PIC .99
.15
AMT-IN PIC 999V99
32495
AMT-OUT PIC 999.99
324.95
PRICE-IN PIC 999V99
00325
PRICE-OUT PIC 999.99
003.25
TOT-IN PIC 99V99
0003
TOT-OUT PIC 99.99
00.03
Represent implied decimal point.
1
2. Suppression of leading zeros.
A leading zeros are the zeros appears on the leftmost side of a
numeric field, for example 00543 has two leading zeros. The edit
symbol(Z) is used to suppress the leading zeros and to replace them
with blanks or spaces. Zeros may be suppressed only if they precede a
significant digit. Since numeric positions to the right of decimal point
are significant even when they are zeros, we will not perform
suppression on them. One exception to this rule is the use of the edit
symbol Z for the whole decimal number, for example PIC ZZZ.ZZ is
allowed in COBOL and this will suppress zeros to the right of a decimal
point only if the entire field is zero.
Each Z represents one storage position that may accept data from the
sending field. If the number has no leading zeros then it will be printed
as is. You have the choice to suppress any amount of leading zeros.
The use of Z’s must precede the use of 9’s when both are combined.
For example using PIC ZZ9Z.99 is incorrect.
Examples
Sending Field
UNIT-IN PIC 9999
0015
Report item receiving field
UNIT-OUT PIC ZZZZ
bb15
UNIT-IN PIC 9999
5412
UNIT-OUT PIC ZZZZ
5412
AMT-IN PIC 999V99
02495
AMT-OUT PIC ZZZ.99
b24.95
AMT-IN PIC 999V99
52433
AMT-OUT PIC ZZZ.99
524.33
PRICE-IN PIC 999V99
45302
PRICE-OUT PIC ZZZ.99
453.02
TOT-IN PIC 999V99
00303
TOT-OUT PIC Z99.99
b03.03
TOT-IN PIC 999V99
45225
TOT-OUT PIC Z99.99
452.25
SUBTOT-IN PIC 999V99
45200
SUBTOT-OUT PIC ZZZ.ZZ
452.00
SUBTOT-IN PIC 999V99
00000
SUBTOT-OUT PIC ZZZ.ZZ
bbbbbb
2

3. Printing (or displaying ) dollar sings and commas.
The editing symbols Dollar sign ($) and comma (,) are placed in the place
where they are desired. Comma will automatically revert to a space if it is
not required in the printed report item.
Examples:
Sending Field
Report item receiving field
AMNT-IN PIC 9999
AMNT-OUT PIC $9999
0015
$0015
TOT-TAX-IN PIC 9999
5412
TOT-TAX-OUT PIC $Z,ZZZ
$5,412
TOT-TAX-IN PIC 9999
0512
TOT-TAX-OUT PIC $ZZZZ
$b512
AMT-IN PIC 9999V99
312495
AMT-OUT PIC $Z,ZZZ.99
$3,124.95
AMT-IN PIC 9999V99
002433
AMT-OUT PIC $Z,ZZZ.99
$b bb24.33
PRICE-IN PIC 9999V99
145302
PRICE-OUT PIC $Z,ZZZ.99
$1,453.02
SUBTOT-IN PIC 9999V99
845200
SUBTOT-OUT PIC $Z,ZZZ.ZZ
$8,452.00
4. Printing (or displaying) an asterisks for check protection.
An asterisks is called check protection symbol that is used in place of
blanks when leading zeros are to be suppressed. In this situation we
will use the editing symbol (*) instead of (Z).
Examples:
Sending Field
AMNT-IN PIC 9999
0015
Report item receiving field
AMNT-OUT PIC $****
$**15
TOT-TAX-IN PIC 9999
5412
TOT-TAX-OUT PIC $*,***
$5,412
TOT-TAX-IN PIC 9999
0512
TOT-TAX-OUT PIC $*,***
$**512
AMT-IN PIC 9999V99
002495
AMT-OUT PIC $*,***.99
$***24.95
3
PRICE-IN PIC 9999V99
145302
PRICE-OUT PIC $*,***.99
$1,453.02
SUBTOT-IN PIC 9999V99
845200
SUBTOT-OUT PIC $*,***.99
$8,452.00
5. Printing (or displaying) plus or minus signs.
When an elementary sending field may be negative the PIC clause
must be leaded with (S), otherwise the complier will consider it as
unsigned.
In order to print a minus sign in an edit item when the sending field is
negative we use the edit symbol (-), this symbol may be placed to the
right or to the left of a report item. However if the sending filed is
positive a blank will be placed instead.
When a sign is required for both negative and positive report items,
then the edit symbol (+) is used. The sending field still must have PIC
clause with leading (S).
The computer use the rightmost position for storing the sign along with
low order digit.
Examples:
Sending Field
AMNT-IN PIC S9999
Report item receiving field
AMNT-OUT PIC Z,ZZZ-

2543
2,543 -
AMNT-IN PIC S9999
AMNT-OUT PIC -Z,ZZZ

2543
-2,543
TOT-TAX-IN PIC S9999
TOT-TAX-OUT PIC $Z,ZZZ-

2543
$2,543-
TOT-TAX-IN PIC S9999
TOT-TAX-OUT PIC Z,ZZZ-

2543
2,542b
AMT-IN PIC S9999V99
AMT-OUT PIC $*,***.99-

0 555 4 3
$**555.43b
4
6. Printing (or displaying) debit or credit symbols for accounting
applications.
The edit symbols (DB) for debit or (CR) for credit may be used in place
of the minus sign for accounting applications; they must be placed to
the right of the report item.
If an amount is to be debited form a negative account DB will be used,
if an amount is to be credited to an account then CR will be used.
If the field is either unsigned or positive then neither will be used and
each will be replaced with two blanks. Remember that each of CR and
DB are presented in two positions, where as a minus is represented in
one position.
Sending Field
AMNT-IN PIC S9999
Report item receiving field
AMNT-OUT PIC Z,ZZZCR

2543
AMNT-IN PIC S9999
2,543CR
AMNT-OUT PIC Z,ZZZDB

2543
TOT-TAX-IN PIC S9999
-,543 DB
TOT-TAX-OUT PIC Z,ZZZCR

2543
TOT-TAX-IN PIC S9999
$2,543bb
TOT-TAX-OUT PIC Z,ZZZDB

2543
2,542bb
7. Printing (or displaying) spaces zeros or slashes as separators
within fields.
Any field whether numeric or nonnumeric may be edited by placing
blanks, zeros and/or slashes as separators within the report item. The
edit symbols (B for blank), (0 for zero) and (/ for slash) are used for this
purpose by placing them in the desired position.
5
Examples:
Sending Field
SIN-IN PIC 9(9)
123456789
Report item receiving field
SIN-OUT PIC 999B999B999
123b456b789
NAME-IN PIC X(8)
RSJOHN
NAME-OUT PIC XBXBx(8)
RbSbJOHNbb
DATE-IN PIC 9(8)
01022002
AMT-IN100S PIC 999
495
DATE-OUT PIC 99/99/9999
01/02/2002
AMT-OUT100S PIC 99900
49500
Editing using floating Strings:
Using floating stings a leading editing character such as plus, minus or dollar
sign may appears in the position directly preceding the first significant digit of
a report printed item. The use of floating string will cause the suppression of
leading zeros and comma and in the same time force the respective floating
character to appear in the position adjacent to the first significant digit. Only
one floating character is allowed in any report item.
Examples
Sending Field
AMNT-IN PIC 9(5)V99
0000400
Report item receiving field
AMNT-OUT PIC $$$$$$.99
$4.00
DEBT-IN PIC S9(4)
DEBT-OUT PIC -----

0003
DEBT-IN PIC S9(4)
-3
DEBT-OUT PIC +++++

0003
+3
AMT-IN PIC 999V99
00000
AMT-OUT PIC $$$.99
$.00
AMT-IN PIC 999
000
AMT-OUT PIC ++++
+
There is an option that can be used to display (or print ) spaces when
the entire sending field consist of zeros. COBOL expression
BLANK WHEN ZERO is used for such reason. When this option is
used with a report item, the normal rules of editing will be followed
depending on the editing symbols in the PIC clause, if the sending field
6
is zero then spaces will be placed in the report item. The only
restriction is that we don’t use this option with the editing symbol (*).
For example
Sending Field
AMNT-IN PIC S9(2)
04
Report item receiving field
AMNT-OUT PIC +++ BLANK WHEN ZERO
+4
AMNT-IN PIC S9(2)
00
AMNT-OUT PIC +++ BLANK WHEN ZERO
bbb
Printing reports and record description entry :
A record is a unit of information that consists of related data items called
fields.
If all the records in a file have the same length and format, the records are
called fixed length records. One record description entity will be defined in
the file description entry of the FILE SECTION.
If a file may consist of records of deferent length and format then a record
description will be needed for each one. When printing or displaying reports,
the report may contain items like report header, pages header, column
headers, detail lines, and footers. In such situation the output record is no
longer of fixed length, so in the FILE SECTION we define the input file as
follow:
FD
01
OUTPUT-FILE.
OUTPUT-REC
PIC X(132).
And all the required records format then is defined in the WORKINGSTORAGE SECTION. In the PROCEDRUE DIVISION records will be moved
from the WORKING-STORAGE area to the output record after been
formatted in the desired way. The record layout forms are used to describe
the format of input and output data, where as printer spacing charts help to
format printed output.
Data stored in the WORKING-STORAGE area must be transmitted to the
print area and then either printed or saved on a file, MOVE statement is used
for this purpose as follow :
MOVE HEADING-1 TO OUTPUT-REC
WRITE OUTPUT-REC
Or we can use WRITE… FROM statement as follow:
WRITE OUTPUT-REC FROM HEADING-1
7
Some other Options:
 JUSTIFIED RIGHT
Used as part of PIC clause of alphanumeric field to right justify the filed
contents instead of left justifying.
 ADVANCING
Printing or displaying quotation marks
Summary:
The following symbols are used with each specified field type
Alphanumeric
X
Numeric
9
V
S
Alphanumeric-edited
X
/
B
Numeric-edited
9
Z
*
,
/
.
+
$
B
0
CR
DB
Displaying the date in a report:
You can use the keyword DATE in COBOL to access the current date. It
stores the current date as a six digits field as yymmdd.
So if we want to display the current day in our report, we can code the
following in the procedure division:
ACCEPT WS-DATE FROM DATE
Where WS-DATE can be defined as follows in the WORKING-STORAHE
SECTION:
01
WS-PAGE-HEAD.
05
WS_DATE.
10 WS-YEAR
10 WS-MONTH
PIC 99.
PIC 99.
8
10 WS-DAY
05
05
WS-TITLE
PIC 99.
PIC X(5).
PIC X(12) VALUE ‘Report title’.
If you want to display the date as dd/mm/yy for example you will need to
defined another working area
01
WS-PAGE-HEAD.
05
WS_DATE-OUT.
10 WS-DAY-OUT
PIC 99.
10
PIC X VALUE ‘/’.
10 WS-MONTH-OUT
PIC 99.
10
PIC X VALUE ‘/’.
10 WS-YEAR-OUT
PIC 99.
05
PIC X(5).
05
WS-TITLE PIC X(12) VALUE ‘Report title’.
01
WS_DATE.
05 WS-YEAR
05 WS-MONTH
05 WS-DAY
Then in the procedure division
PIC 99.
PIC 99.
PIC 99.
ACCEPT WS-DATE FROM DATE
MOVE WS-YEAR TO WS-YEAR-OUT
MOVE WS-MONTH TO WS-MONTH-OUT
MOVE WS-DAY TO WS-DAY-OUT
If our output record is OUTPUT-REC we can code:
WRITE OUTPUT-REC FROM WS-PAGE-HEAD
Through the revision of COBOL standard in 1989 , an enhancements was
added to enable storing the date in 8 digits to allow 4 digits for the year.
FUNCTION CURRENT-DATE is an intrinsic function added to COBOL 85
compilers to make them Y2k compliant. The field that will store the date must
be a group item subdivided into elementary fields for year, month and day as
shown bellow:
01
WS_DATE.
05 WS-YEAR
PIC 9999.
05 WS-MONTH
PIC 99.
05 WS-DAY
PIC 99.
In the PROCEDURE DIVOSION we code:
MOVE FUNCTION CURRENT-DATE TO WS_DATE.
Reference: Textbook Ch6 pg202-225.
9
Download