Book Read Free

Michael Coughlan

Page 24

by Beginning COBOL for Programmers-Apress (2014) (pdf)


  01 FinalTotalLine.

  02 FILLER PIC X(19) VALUE " Total Students:".

  02 PrnFinalTotal PIC ZZ,ZZ9.

  01 CourseTotal PIC 9(4).

  01 FinalTotal PIC 9(5).

  01 PrevCourseCode PIC X(5).

  prOGraMMING eXerCISe 2

  Change the program you wrote for programming exercise 1 so that it now writes the report to a print file.

  the answer to this exercise is given below. Because exercise 1 is substantially the same as exercise 2, the same answer should serve both.

  prOGraMMING eXerCISeS 1 aND 2: aNSWer

  IDENTIFICATION DIVISION.

  PROGRAM-ID. Listing8-5.

  AUTHOR. Michael Coughlan.

  * This program processes the first year students entrants file to produce

  * a summary report sequenced on ascending Course Code that shows the number

  * of first year students* in each course.

  * The Entrants File is a sequential file sequenced on ascending CourseCode.

  ENVIRONMENT DIVISION.

  INPUT-OUTPUT SECTION.

  FILE-CONTROL.

  SELECT EntrantsFile ASSIGN TO "Listing8-5-Entrants.Dat"

  ORGANIZATION IS LINE SEQUENTIAL.

  SELECT SummaryReport ASSIGN TO "Listing8-5-Summary.Rpt"

  ORGANIZATION IS SEQUENTIAL.

  DATA DIVISION.

  FILE SECTION.

  FD EntrantsFile.

  01 StudentRecord.

  88 EndOfEntrantsFile VALUE HIGH-VALUES.

  02 StudentId PIC 9(8).

  02 CourseCode PIC X(5).

  02 Gender PIC X.

  179

  Chapter 8 ■ advanCed Sequential FileS

  FD SummaryReport.

  01 PrintLine PIC X(35).

  WORKING-STORAGE SECTION.

  01 HeadingLine1 PIC X(31) VALUE " First Year Entrants Summary".

  01 HeadingLine2 PIC X(31) VALUE " Course Code NumOfStudents".

  01 CourseLine.

  02 FILLER PIC X(5) VALUE SPACES.

  02 PrnCourseCode PIC X(5).

  02 FILLER PIC X(10) VALUE SPACES.

  02 PrnCourseTotal PIC BBZZ9.

  01 FinalTotalLine.

  02 FILLER PIC X(19) VALUE " Total Students:".

  02 PrnFinalTotal PIC BZ,ZZ9.

  01 CourseTotal PIC 9(4) VALUE ZEROS.

  01 FinalTotal PIC 9(5) VALUE ZEROS.

  01 PrevCourseCode PIC X(5) VALUE ZEROS.

  PROCEDURE DIVISION.

  ProduceSummaryReport.

  OPEN INPUT EntrantsFile

  OPEN OUTPUT SummaryReport

  WRITE PrintLine FROM HeadingLine1 AFTER ADVANCING PAGE

  WRITE PrintLine FROM HeadingLine2 AFTER ADVANCING 2 LINES

  READ EntrantsFile

  AT END SET EndOfEntrantsFile TO TRUE

  END-READ

  PERFORM UNTIL EndOfEntrantsFile

  MOVE CourseCode TO PrnCourseCode, PrevCourseCode

  MOVE ZEROS TO CourseTotal

  PERFORM UNTIL CourseCode NOT = PrevCourseCode

  ADD 1 TO CourseTotal, FinalTotal

  READ EntrantsFile

  AT END SET EndOfEntrantsFile TO TRUE

  END-READ

  END-PERFORM

  MOVE CourseTotal TO PrnCourseTotal

  WRITE PrintLine FROM CourseLine AFTER ADVANCING 1 LINE

  END-PERFORM

  MOVE FinalTotal TO PrnFinalTotal

  WRITE PrintLine FROM FinalTotalLine AFTER ADVANCING 2 LINES

  CLOSE EntrantsFile, SummaryReport

  STOP RUN.

  180

  Chapter 9

  Edited Pictures

  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.

  Formatting Output

  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.

  181

  Chapter 9 ■ edited piCtures

  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

  Immediate Editing

  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.

&
nbsp; Example Program

  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.

  182

  Chapter 9 ■ edited piCtures

  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:

  • When NumericValue is moved to Edit1, the assumed decimal point in NumericValue aligns

  with the actual decimal point in Edit1, and the actual decimal-point character is inserted.

  In addition, commas are inserted where they are specified in the edited picture.

  183

  Chapter 9 ■ edited piCtures

  • The zero-suppression symbol Z in Edit2 modifies the data so that leading zeros are replaced

  with spaces.

  • In Edit3, the edit symbols cause the dollar sign to be inserted and the leading zeros to be

  replaced with asterisks.

  • The plus-sign symbols in Edit4 cause the sign to float up against the first non-zero digit.

  • The data in Edit5 is similarly modified, but using the dollar sign.

  • The editing specified for Edit6 inserts two zeros after the decimal point. This means when the

  data is moved into Edit6 and there is alignment along the decimal point, there is no room for

  the digits 9 and 5, which are truncated.

  • Edit7 shows how the slash character can be inserted into a number. The slash is used to good

  effect when formatting dates, but it is used here to show that it is not restricted to date values.

  • In Edit8, the zero edit symbol is used to insert zeros into the middle of the number.

  • In Edit9, the blank symbol B is used to insert spaces or blanks into the middle of the number.

  This can be useful for formatting dates or for aligning report headings or values.

  Types of Editing

  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:

  • Simple insertion

  • Special insertion

  • Fixed insertion

  • Floating insertion

  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:

  • Zero-suppression and replacement with spaces

  • Zero-suppression and replacement with asterisks (*)

  ■ 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.

  184

  Chapter 9 ■ edited piCtures

  Editing Symbols

  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

  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.

  Simple-Insertion Editing

  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.

  How the Symbols Work

  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.

  Simple-Insertion Examples

  Table 9-3 gives some simple-insertion example PICTURE strings, shows the formatting that these edited pictures apply to data values, and provides a comment that explains what is done.

  185

  Chapter 9 ■ edit
ed piCtures

  Table 9-3. Simple-Insertion Examples

  Sending

  Receiving

  Comments

  Picture

  Data

  Picture

  Result

  PIC X(8)

  MikeRyan

  PIC X(4)BBX(4)

  MikeRyan

  Spaces are inserted.

  Size = 10 characters.

  PIC X(9)

  10Jan2013

  PIC XX/XXX/(4)

  10/Jan/2013

  Slashes are inserted.

  Size = 11 characters.

  PIC 9(6)

  123456

  PIC 999,999

  123,456

  Comma is inserted.

  Size = 7 characters.

  PIC 9(6)

  000045

  PIC 9(3),9(3)

  000,045

  Comma is inserted. Note the leading zeros.

  Size = 7 characters.

  PIC 9(6)

  000045

  PIC ZZZ,ZZZ

  45

  Leading zeros are replaced with spaces

  (represented by ). Because there is a zero to

  the left of the comma, it is replaced by a space.

  Size = 7 characters.

  PIC 9(6)

  000345

  PIC ***,***

  ****345

  Zero-suppression and the zero to the left of the

  comma cause the comma to be replaced by

  an asterisk.

  Size = 7 characters.

  PIC 9(6)

  002345

  PIC ***,***

  **2,345

  Zero-suppression is used, but there is a

  non-zero to the left of the comma, so the

  comma is inserted.

  Size = 7 characters.

  PIC 9(8)

  12252013

  PIC 99B99B9999

  12252013

  Spaces are inserted.

  Size = 10 characters.

  PIC 9(8)

  12252013

  PIC 99/99/9999

  12/25/2013

  Slashes are inserted.

  Size = 10 characters.

  PIC 9(6)

  7654329

  PIC 990099

  430029

  No explicit decimal point in either the sending

  or receiving field means each is treated as if it

 

‹ Prev