In the previous chapter, you saw how a printed report may be created by sending data directly to the printer or to a print file. In this chapter, you continue your exploration of printed output by examining how data may be formatted for output.
Most users of the data produced by a COBOL report program are not content with the simple raw, unformatted data. Unformatted data is difficult to read, so users want it presented in a way that makes it easier to understand. This is especially true of numeric data. Users may want numeric values separated into thousands, and they may want leading zeros to be suppressed. If the report contains currency values, then users may want the currency symbol to be printed, and they may want the symbol floated up against the first non-zero digit. In COBOL, all these effects and more can be achieved using edited pictures.
Edited Pictures
Edited pictures are picture clauses that format data intended for output to a screen or a printer. To enable the data items to be formatted, special symbols are embedded in the picture clause. These symbols supplement the basic 9, X, A, V, and S picture clause symbols. The additional symbols are referred to as edit symbols, and picture clauses that include edit symbols are called edited Pictures. The term edit is used because the edit symbols cause the data in the edited item to be changed or “edited.”
When numeric data is moved into an edited numeric data item, it obeys the rules for numeric moves, with decimal-point alignment and zero-filling or truncation as necessary. Although an edited numeric data item cannot be used as an active operand in a computation, it may be used as the receiving field of a computation. That is, it may be used to the right of the word GIVING.
The last chapter ended with an example that showed a number of the formatting effects that may be applied to a data value. The example did not show how those effects were achieved. You start this chapter by revisiting that example and examining a program that shows how edited pictures were used to achieve those effects. After you have seen how edited pictures are used in a program, this chapter explores the topic of edited pictures in detail. You examine the different kinds of editing that may be applied to data, and you expand your knowledge of the special symbols used to create edited pictures.
Table 9-1 restates the example given in Table 8-1 in the previous chapter. This example shows some of the different kinds of formatting that may be applied to a data value. Among the effects are numeric values divided into thousands by commas, suppression of leading zeros, and the plus sign and the currency symbol floating up against the first non-zero value.
Table 9-1. Edited Picture Formatting Effects
Effect |
Value |
---|---|
Original value |
00014584.95 |
With commas inserted |
00,014,584.95 |
With zero-suppression added |
14,584.95 |
With check security and currency symbol added |
$***14,584.95 |
With floating + sign |
+14,584.95 |
With floating currency symbol |
$14,584.95 |
With zeros inserted after the decimal point |
$14,584.00 |
With slashes inserted in the middle of the number |
00/014/584.95 |
With three zeros inserted in the number |
00014000584.95 |
With three blanks inserted in the number |
00014 584.95 |
The most important thing to know about an edited picture is that the data formatting is not done when the edited data is output to a printer or a computer screen; it is immediate. The moment data is moved into an edited item, the data itself is modified according to the formatting instructions specified by the edited picture.
It can be very useful to know that when a data value is moved into an edited item, it is immediately formatted; once you know that, you can think of a number of manipulations that you can do to the edited data to achieve interesting effects. For instance, you could use COBOL string-handling to replace the floating dollar sign with the Euro, Yen, or other currency symbol; or you might replace the slash symbol in a date with the hyphen or some other separator. Later, this chapter returns to this idea and looks at some examples.
Listing 9-1 is a simple program that shows how the formatting effects of Table 9-1 were achieved. This program has been pared down to its essential elements so that you can concentrate on the editing effects. The program uses the DISPLAY statement to output the edited data to the screen.
Listing 9-1. Using Edited Pictures to Format Data for Output
IDENTIFICATION DIVISION.
PROGRAM-ID. Listing9-1.
AUTHOR. Michael Coughlan.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NumericValue PIC 9(8)V99 VALUE 00014584.95.
01 Edit1 PIC 99,999,999.99.
01 Edit2 PIC ZZ,ZZZ,ZZ9.99.
01 Edit3 PIC $*,***,**9.99.
01 Edit4 PIC ++,+++,++9.99.
01 Edit5 PIC $$,$$$,$$9.99.
01 Edit6 PIC $$,$$$,$$9.00.
01 Edit7 PIC 99/999/999.99.
01 Edit8 PIC 99999000999.99.
01 Edit9 PIC 99999BBB999.99.
PROCEDURE DIVISION.
Begin.
MOVE NumericValue TO Edit1
DISPLAY "Edit1 = " Edit1
MOVE NumericValue TO Edit2
DISPLAY "Edit2 = " Edit2
MOVE NumericValue TO Edit3
DISPLAY "Edit3 = " Edit3
MOVE NumericValue TO Edit4
DISPLAY "Edit4 = " Edit4
MOVE NumericValue TO Edit5
DISPLAY "Edit5 = " Edit5
MOVE NumericValue TO Edit6
DISPLAY "Edit6 = " Edit6
MOVE NumericValue TO Edit7
DISPLAY "Edit7 = " Edit7
MOVE NumericValue TO Edit8
DISPLAY "Edit8 = " Edit8
MOVE NumericValue TO Edit9
DISPLAY "Edit9 = " Edit9
STOP RUN.
The data item NumericValue is a decimal number. The V in the picture clause indicates the position of the assumed decimal point; but the actual decimal point, which is a text character, is not held in the data item. To display or print an actual decimal point, you must use an edited picture containing the decimal-point editing symbol. If you examine the Edit1 data item in Listing 9-1, you see that the V, which normally indicates the position of the decimal point, has been replaced by the actual decimal-point character.
When the value in NumericValue is moved into the edited item, the data is immediately modified according to the formatting specified by the edit symbols. A brief explanation of the effect of moving data from NumericValue to each of the edited items is given next:
There are two basic types of editing in COBOL: insertion editing, and suppression and replacement editing. Insertion editing modifies the data value by inserting additional characters into the data. This type of editing has the following subcategories:
Suppression and replacement editing modifies the data value by suppressing leading zeros and replacing them with a replacement character. This type of editing has the following subcategories:
COBOL Detail Zero-suppression and replacement with spaces can also be achieved by using the BLANK WHEN ZERO clause. This clause can sometimes be useful because it may be used with a picture clause that contains editing symbols (except the asterisk [*] replacement symbol). For instance, 01 BlankedNumber PIC +$$$,$$9 BLANK WHEN ZERO.
Special picture symbols are used in an edited-picture clause to specify the formatting required. Table 9-2 shows the special picture clause symbols used in edited pictures and categorizes them by the type of editing they are used for.
Table 9-2. Editing Symbols
Edit Symbol |
Editing Type |
---|---|
, B 0 / |
Simple insertion |
. |
Special insertion |
+ - CR DB $ |
Fixed insertion |
+ - $ |
Floating insertion |
Z * |
Suppression and replacement |
Insertion editing is so named because the edit symbol is inserted into the data value at the same position it occupies in the picture clause. As mentioned earlier, there are four types of insertion editing: simple insertion, special insertion, fixed insertion, and floating insertion. The following sections explore these types of editing in more detail.
A simple-insertion edited picture consists of a PICTURE string that specifies the relevant insertion character(s) in the required character position. When a data value is moved into the edited item, the insertion characters are inserted into the item at the position specified in the PICTURE. Simple insertion may be used with both numeric-edited and alphanumeric-edited data items.
As shown in Table 9-2, the comma, the blank or space, the zero, and the slash are the simple-insertion editing symbols. In simple insertion, all the inserted characters count toward the number of characters printed or displayed. For instance, an item described as PIC 9999/99/99 occupies ten character positions when printed. You need to be aware of this when designing report layouts.
The comma symbol (,) instructs the computer to insert a comma at the character position where the symbol occurs. The comma counts toward the size of the printed item. When used with zero-suppression and replacement or floating insertion, the comma operates in a special way: if all characters to the left of the comma are zeros, the comma is replaced with the appropriate character (currency symbol, asterisk, or space).
The space or blank (B), slash (/), and zero (0) symbols instruct the computer to insert the appropriate character at the position where the symbol occurs in the PICTURE string.
The only special-insertion symbol is the decimal point. The decimal-point insertion symbol has the effect of inserting an actual decimal point into the edited item. This type of editing is called special insertion because of the effect on data moved into the edited item. Data sent to the edited field is aligned along the decimal point, with zero-filling or truncation as required. The decimal point is inserted in the character position where the symbol occurs, and there may be only one decimal point. The decimal-point symbol cannot be mixed with either the V (assumed decimal point) or the P (scaling position) symbol. The purpose and operation of the P symbol is explored later in this chapter.
Table 9-4 gives some special-insertion example picture strings and shows the formatting that these edited pictures apply to data values. You probably noticed that the last example in Table 9-3 was an example of special insertion as well as simple insertion.
Table 9-4. Special-Insertion Editing Examples
Fixed-insertion editing is so named because it inserts the edit symbol in a fixed position at the beginning or end of the edited item. The fixed insertion editing symbols are as follows:
Like the other insertion edit symbols, the fixed-insertion symbols count toward the size of the printed item.
The plus (+) and minus (-) symbols must be placed in the first or last character position of the PICTURE string. The operation of the plus and minus edit symbols is not as straightforward as it may appear. The rules governing their operation are as follows:
CR and DB stand for credit and debit, respectively. But what a credit is and what a debit is depends on which side of the balance sheet you are on. Therefore, the rule with the CR and DB symbols is that both are inserted only if the value is negative. CR and DB count toward the data-item size, occupy two character positions, and may only appear in the last character position of the edit PICTURE string.
The currency symbol (usually $) must be one of the leading characters of the edit PICTURE string. It may be preceded by a plus or a minus sign.
The default currency symbol is the dollar sign ($); but as shown in Listing 9-2, it may be changed to a different symbol by the CURRENCY SIGN IS clause, in the SPECIAL-NAMES paragraph, CONFIGURATION SECTION, ENVIRONMENT DIVISION.
Listing 9-2. Using the CURRENCY SIGN Clause to Change the Currency Symbol
IDENTIFICATION DIVISION.
PROGRAM-ID. Listing9-2.
AUTHOR. Michael Coughlan.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SPECIAL-NAMES.
CURRENCY SIGN IS "£".
DATA DIVISION.
WORKING-STORAGE SECTION.
01 Edit1 PIC £££,££9.99.
PROCEDURE DIVISION.
Begin.
MOVE 12345.95 TO Edit1
DISPLAY "Edit1 = " Edit1
STOP RUN.
In Listing 9-3, multiple currency sign declarations are used to create a currency converter program. Several CURRENCY SIGN declarations are made (note that while there are several clauses there is only one sentence and hence one period) and then each edited picture uses the appropriate currency symbol.
Listing 9-3. Using Multiple CURRENCY SIZE clauses
IDENTIFICATION DIVISION.
PROGRAM-ID. Listing9-3.
AUTHOR. Michael Coughlan.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SPECIAL-NAMES.
CURRENCY SIGN IS "£"
CURRENCY SIGN IS "$"
CURRENCY SIGN IS "¥".
DATA DIVISION.
WORKING-STORAGE SECTION.
01 DollarValue PIC 9999V99.
01 PrnDollarValue PIC $$$,$$9.99.
01 PrnYenValue PIC ¥¥¥,¥¥9.99.
01 PrnPoundValue PIC £££,££9.99.
01 Dollar2PoundRate PIC 99V9(6) VALUE 0.640138.
01 Dollar2YenRate PIC 99V9(6) VALUE 98.6600.
PROCEDURE DIVISION.
Begin.
DISPLAY "Enter a dollar value to convert :- " WITH NO ADVANCING
ACCEPT DollarValue
MOVE DollarValue TO PrnDollarValue
COMPUTE PrnYenValue ROUNDED = DollarValue * Dollar2YenRate
COMPUTE PrnPoundValue ROUNDED = DollarValue * Dollar2PoundRate
DISPLAY "Dollar value = " PrnDollarValue
DISPLAY "Yen value = " PrnYenValue
DISPLAY "Pound value = " PrnPoundValue
STOP RUN.
The problem with fixed-insertion editing is that data formatted using it can be somewhat unsightly. Values like $00019,825.75 and -0000135 are more acceptably presented as $19,825.75 and -135. What makes these formats more presentable is that the leading zeros have been suppressed and the editing symbol has been “floated” up against the first non-zero digit. In COBOL, this effect can be achieved using floating insertion. Floating insertion can only be applied to numeric-edited data items.
The floating-insertion symbols are the plus and minus signs and the currency symbol. Floating insertion suppresses leading zeros and floats the insertion symbol up against the first non-zero digit. Every floating symbol counts toward the size of the printed item. Each floating-insertion symbol—with the exception of the leftmost symbol, which is always inserted—is a placeholder that may be replaced by a space or a digit. This means at least one symbol is always inserted, even though this may be at the cost of truncating the number.
The examples in Table 9-7 show how you can use floating-insertion editing. You should pay particular attention to how floating insertion deals with the comma when this symbol is combined with the floating-insertion symbols (see the second example).
Table 9-7. Floating-Insertion Editing with Plus, Minus, and the Currency Symbol
Suppression-and-Replacement Editing
Suppression-and-replacement editing is used to replace leading zeroes from the value to be edited with the replacement symbol. Like floating insertion, suppression and replacement can only be applied to numeric-edited data items. There are two varieties of suppression-and-replacement editing:
The suppression and replacement symbols are the letter Z and the asterisk (*). Using Z in an edited picture instructs the computer to suppress a leading zero in that character position and replace it with a space. Using * in an edited picture instructs the computer to replace a leading zero with an asterisk. The picture clause symbol 9 cannot appear to the left of the replacement symbols (Z or *). If all the character positions in a data item are Z editing symbols and the sending item is 0, then only spaces will be printed. Replacement with spaces is done for aesthetic reasons, but replacement with asterisks is often done as a security measure on checks.
Suppression-and-Replacement Examples
Table 9-8 shows how you can use suppression and replacement. As with the examples in Table 9-7, you should pay particular attention to how suppression-and-replacement editing deals with the comma when this symbol is combined with the replacement symbols.
Table 9-8. Suppression-and-Replacement Editing Examples
Example 9-1 shows some print lines. Note how the edit symbol B is used for spacing. If this were not done, additional data items filled with spaces, as shown in Example 9-2, would have to be used.
Example 9-1. Example Print Lines Containing Edited Pictures
01 Cust-Sales-Line.
02 Prn-Cust-Name PIC X(20).
02 Prn-Cust-Id PIC BBB9(5).
02 Prn-Cust-Sales PIC B(5)ZZ9.
02 Prn-Qty-Sold PIC B(5)ZZ,ZZ9.
02 Prn-Sales-Value PIC BBBB$$$,$$9.99.
01 Total-Sales-Line.
02 FILLER PIC X(33) VALUE SPACES.
02 FILLER PIC X(19) VALUE "TOTAL SALES :".
02 Prn-Total-Sales PIC B(6)ZZ,ZZ9.
Example 9-2. Spacing with Space-Filled Data Items
01 Cust-Sales-Line.
02 Prn-Cust-Name PIC X(20).
02 FILLER PIC XXX VALUE SPACES.
02 Prn-Cust-Id PIC 9(5).
02 FILLER PIC X(5) VALUE SPACES.
02 Prn-Cust-Sales PIC ZZ9.
02 FILLER PIC X(5) VALUE SPACES.
02 Prn-Qty-Sold PIC ZZ,ZZ9.
02 FILLER PIC X(4) VALUE SPACES.
02 Prn-Sales-Value PIC $$$,$$9.99.
I noted earlier that the moment a value is placed into an edited data item, the value is modified according the formatting specified by the edit string. This section examines examples that show some of the interesting effects you can achieve by taking advantage of the way editing works.
In Figure 9-1, the data item SouthAfricanPay uses the dollar sign as the floating-insertion symbol. Obviously, this is a problem. The currency of South Africa is the Rand, which is represented by the character R. What you want is a floating R character rather than a floating dollar sign. Unfortunately, you cannot change the currency symbol to R using the CURRENCY SIGN clause: restrictions are placed on the characters that can be used with that clause, and R is one of the restricted characters. So what can you do? Immediate editing gives you the answer. You replace the dollar sign that has floated against the number, with an R.
Figure 9-1. The floating Rand symbol
Listing 9-4 uses zero suppression and replacement by asterisks to create a starred rating system. In this system, a rating of 5 is shown as 5 asterisks, 4 is shown as 4 asterisks, and so on. The COMPUTE statement produces the values 10000, *1000, **100, ***10, ****1, and *****. The INSPECT replaces each 1 and each 0 in Stars with a space. INSPECT Stars CONVERTING "10" TO SPACES is a shorthand way of writing - INSPECT Stars REPLACING ALL "1" BY SPACE, ALL "0" BY SPACE. You'll examine INSPECT in detail in Chapter 15.
Listing 9-4. Starred Rating System
IDENTIFICATION DIVISION.
PROGRAM-ID. Listing9-4.
AUTHOR. Michael Coughlan.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 Stars PIC *****.
01 NumOfStars PIC 9.
PROCEDURE DIVISION.
Begin.
PERFORM VARYING NumOfStars FROM 0 BY 1 UNTIL NumOfStars > 5
COMPUTE Stars = 10 ** (4 - NumOfStars)
INSPECT Stars CONVERTING "10" TO SPACES
DISPLAY NumOfStars " = " Stars
END-PERFORM
STOP RUN.
Some combinations of picture symbols are not permitted. Table 9-9 shows the combinations of symbols that are allowed. You should now be familiar with all these PICTURE symbols except P: the P symbol is a scaling symbol.
Table 9-9. PICTURE String Restrictions
Character |
May Be Followed By |
---|---|
P |
P B 0 / , + - CR DB 9 V |
B |
P B 0 / , . + - CR DB 9 V |
0 |
P B 0 / , . + - CR DB 9 V |
/ |
P B 0 / , . + - CR DB 9 V |
, |
P B 0 / , . + - CR DB 9 V |
. |
B 0 / , . + - CR DB 9 |
+ |
P B 0 / , . + $ 9 V |
- |
P B 0 / , . - $ 9 V |
CR or DB |
Nothing at all |
$ |
P B 0 / , . + - CR DB $ 9 V |
9 |
P B 0 / , . + - CR DB 9 V |
V |
B 0 / , + - CR DB 9 |
The PICTURE Clause Scaling Symbol
The P symbol in a PICTURE clause specifies a decimal-point scaling position. It is used to save storage when the magnitude of a number is significantly larger than the required precision. For instance, suppose you are required to store numbers that contain whole billions. You could use a declaration such as PIC 9(12), which requires 12 characters, or you could use the scaling symbol P to define the item as PIC 999P(9). This definition requires only three characters and can store a value between 001,000,000,000 and 999,000,000,000.
The P symbol is not often used, but a description of how it operates is included here for completeness. By default, when no assumed decimal point is explicitly defined, the data item is treated as if it had a decimal point in the rightmost position. The P symbol allows you to change that by defining the assumed decimal point to be to the left or right of the digits, depending on where the P symbol is placed. Each P symbol represents one decimal scaling position.
In Example 9-3, LargeScaledNumber occupies only three characters of storage but can hold a value as high as 99,900,000. Similarly, ScaledBillions occupies only three characters of storage but can hold a number as large as 999,000,000,000, while SmallScaledNumber can hold a number as small as 0.00000001.
Example 9-3. Scaling, Which Allows Numbers to Be Defined Using Less Storage
01 SmallScaledNumber PIC P(5)999 VALUE .00000423.
01 LargeScaledNumber PIC 999P(5) VALUE 45600000.00.
01 ScaledBillions PIC 999P(9) VALUE ZEROS.
Rules
If the symbol P is used more than once, it can only occur as a contiguous string of Ps at the leftmost or rightmost end of the PICTURE string. The assumed decimal point symbol (V) can be used for clarity, but it has no semantic effect, and when used it must appear to the left of the leftmost P or to the right of the rightmost P. For instance, to clarify where the decimal point is, you could define the Example 9-3 data items as follows:
01 SmallScaledNumber PIC VPPPPP999 VALUE .00000423.
01 LargeScaledNumber PIC 999PPPPPV VALUE 45600000.00.
01 ScaledBillions PIC 999PPPPPPPPPV VALUE ZEROS.
The P symbols do not count toward the size of the item. However, each P counts toward the maximum number of digit positions (18) in a numeric item.
The P symbol cannot be used if the explicit decimal-point edit symbol is used in the PICTURE string.
All computations and other operations performed against scaled data items behave as if the decimal point were in the scaled position. For instance, as shown in Listing 9-5, the result of adding LargeScaledNumber to the data item containing the value 11,111,111.00 is 56,711,111.00.
Listing 9-5. Using the Scaling Symbol P
IDENTIFICATION DIVISION.
PROGRAM-ID. Listing9-5.
AUTHOR. Michael Coughlan.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 SmallScaledNumber PIC VP(5)999 VALUE .00000423.
01 LargeScaledNumber PIC 999P(5)V VALUE 45600000.00.
01 ScaledBillions PIC 999P(9) VALUE ZEROS.
01 SmallNumber PIC 9V9(8) VALUE 1.11111111.
01 LargeNumber PIC 9(8)V9 VALUE 11111111.
01 PrnSmall PIC 99.9(8).
01 PrnLarge PIC ZZ,ZZZ,ZZ9.99.
01 PrnBillions PIC ZZZ,ZZZ,ZZZ,ZZ9.
PROCEDURE DIVISION.
Begin.
MOVE SmallScaledNumber TO PrnSmall
MOVE LargeScaledNumber TO PrnLarge
DISPLAY "Small scaled = " PrnSmall
DISPLAY "Large scaled = " PrnLarge
ADD SmallScaledNumber TO SmallNumber
ADD LargeScaledNumber TO LargeNumber
MOVE SmallNumber TO PrnSmall
MOVE LargeNumber TO PrnLarge
DISPLAY "Small = " PrnSmall
DISPLAY "Large = " PrnLarge
MOVE 123456789012 TO ScaledBillions
MOVE ScaledBillions TO PrnBillions
DISPLAY "Billions = " PrnBillions
STOP RUN.
Summary
This chapter continued the exploration of printed output that began in the last chapter. You discovered how data can be formatted for output using edited pictures. You explored the different kinds of editing supported by COBOL, from simple insertion of the comma, the currency symbol, and the plus and minus signs; to the more sophisticated floating insertion, which floats these last symbols against the number being displayed or printed; to zero-suppression and replacement with asterisks or spaces. You learned that the editing effect is immediate, and you saw some of the interesting post-edit manipulations you can do on the edited data item. Finally, you examined how the PICTURE symbol P may be used to store a very large or very small value in only a few characters of storage.
The next chapter examines some of the problems of processing sequential files. In particular, you look at some of the difficulties of the file-update problem and learn how to write control-break programs.
LANGUAGE KNOWLEDGE EXERCISE
The time has come once more to unlimber those 2B pencils and answer some exercise questions.
a.
b.
c.
d.
PROGRAMMING EXERCISE 1
The Genealogists Society of Ireland wishes to discover the most popular surname used in each of the 26 counties in the Irish Republic. In order to obtain this information, the society has acquired a file containing a subset of data from the most recent census.
Write a program that will process the census file and produce a report that shows, for each county, the most popular surname and the number of times it occurs.
The census file is a standard sequential file with fixed-length fields. Each record contains a census number, a surname, and a county name. The file has been sorted and is now ordered on ascending Surname within ascending CountyName. Each record in the file has the following description:
The report should take the format shown in the following report template. The Count field is a count of the number of times the surname occurs in the county. In the Count field, thousands should be separated using a comma; and the field should be zero-suppressed up to, but not including, the last digit:
Popular Surname Report
CountyName Surname Count
Carlow XXXXXXXXXXXXXXXXXXXX XXX,XXX
Cavan XXXXXXXXXXXXXXXXXXXX XXX,XXX
Clare XXXXXXXXXXXXXXXXXXXX XXX,XXX
:: :: :: :: :: :: :: :: :: :: ::
Westmeath XXXXXXXXXXXXXXXXXXXX XXX,XXX
Wicklow XXXXXXXXXXXXXXXXXXXX XXX,XXX
Wexford XXXXXXXXXXXXXXXXXXXX XXX,XXX
************* end of report ***************
LANGUAGE KNOWLEDGE EXERCISES: ANSWERS
The time has come once more to unlimber those 2B pencils and answer the following exercise questions.
a.
b.
c.
d.
PROGRAMMING EXERCISE 1: ANSWER
The answer to this exercise is found in the next chapter, where it appears an an example.
18.223.107.85