Book Read Free

Michael Coughlan

Page 26

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


  PIC 9(4)

  0317

  PIC Z,Z99

  317

  When there are only zeros to the left of the comma,

  the comma is replaced with the replacement symbol.

  Size= 5 characters.

  PIC 9(4)

  0007

  PIC Z,Z99

  07

  The edited value is 07 because the edit picture does

  not require replacement for the last two digits.

  Size= 5 characters.

  PIC 9(4)

  0000

  PIC ****

  ****

  The value is zero, and the edit string instructs the

  computer to replace the leading zeros with asterisks.

  Size= 4 characters.

  PIC 9(4)

  0083

  PIC ****

  **83

  Leading zeros are replaced with asterisks.

  Size= 4 characters.

  ( continued)

  193

  Chapter 9 ■ edited piCtures

  Table 9-8. ( continued)

  Sending

  Receiving

  Comments

  Picture

  Data

  Picture

  Result

  PIC 9(4)

  8317

  PIC $*,**9.00

  $8,317.00

  No replacement occurs, but the currency symbol,

  comma, and zeros are inserted.

  Size = 9 characters.

  PIC 9(4)

  0317

  PIC $*,**9.00

  $**317.00

  The comma is replaced with an asterisk.

  Size = 9 characters.

  PIC 9(4)

  0017

  PIC $*,999.00

  $**017.00

  The comma is replaced with an asterisk.

  Size = 9 characters.

  Example Print Lines

  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.

  194

  Chapter 9 ■ edited piCtures

  Immediate Editing

  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

  Example Program

  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.

  195

  Chapter 9 ■ edited piCtures

  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.

  PICTURE String Restrictions

  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.

  196

  Chapter 9 ■ edited piCtures

  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 PI
C 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.

  197

  Chapter 9 ■ edited piCtures

  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.

  198

  Chapter 9 ■ edited piCtures

  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.

  1. For each part, examine the formatted results produced for the various data values. deduce

  what the edited picture would have to be to produce the formatted results shown from the

  data values given.

  a.

  Sending

  Data

  Result

  Edited Picture

  PIC 9(5)

  12345

  12345

  01234

  *1234

  00123

  **123

  00012

  **012

  b.

  Sending

  Data

  Result

  Edited Picture

  PIC 9(6)

  412345

  $12345

  000123

  **$123

  000001

  ****$1

  000000

  ******

  c.

  Sending

  Data

  Result

  Edited Picture

  PIC 9(6)V99

  012345

  $123.00

  000123

  $**1.00

  000025

  $**0.00

  000000

  $**0.00

  199

  Chapter 9 ■ edited piCtures

  d.

  Sending

  Data

  Result

  Edited Picture

  PIC S9(4)

  1234

  +1234

  -0012

  **-12

  0004

  ***+4

  0000

  *****

  2. show the formatted result that will be produced when the data value is moved to the

  edited picture.

  Sending

  Data

  Result

  Edited Picture

  9(6)

  000321

  PIC ZZZ,999

  9(6)

  004321

  PIC ZZZ,999

  9(6)

  000004

  PIC ZZZ,999

  9(6)

  654321

  PIC ZZZ,ZZZ.00

  9999V99

  654321

  PIC ZZZ,ZZZ.ZZ

  9999V99

  004321

  PIC $$,$$9.99

  9999V99

  000078

  PIC $$,$$9.99

  9999V99

  000078

  PIC $Z,ZZ9.99

  S9999V99

  000078

  PIC $Z,ZZ9.99CR

  S9999V99

  -045678

  PIC $Z,ZZ9.99CR

  S9(6)

  -123456

  PIC -999,999

  S9(6)

  123456

  PIC -999,999

  S9(6)

  123456

  PIC +999,999

  S9(6)

  -123456

  PIC +999,999

  S9(6)

  001234

  PIC ++++,++9

  9(6)

  123456

  PIC 99B99B99

  9(6)

  001234

  PIC Z(6).00

  9(6)

  000092

  PIC ZZZZZZ00

  X(5)

  123GO

  PIC XBXBXBBXX

  9999V99

  000123

  PIC $***,**9.99

  99999V99

  24123.45

  PIC $$,$$9.99

  200

  Chapter 9 ■ edited piCtures

  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:

  Field

  Type

  Lengthr />
  Value

  CensusNumber

  N

  8

  00000001-99999999

  Surname

  X

  20

  -

  CountyName

  X

  9

  -

  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 ***************

  201

  Chapter 9 ■ edited piCtures

  LaNGUaGe KNOWLeDGe eXerCISeS: aNSWerS

  the time has come once more to unlimber those 2B pencils and answer the following exercise questions.

  1. For each part, examine for formatted results produced for the various data values.

  deduce what the edited picture would have to be to produce the formatted results from

  the data values given.

  a.

  Sending

  Data

  Result

  Edited Picture

  PIC 9(5)

  12345

  12345

  PIC ZZ999

  01234

  *1234

  00123

  **123

  00012

  **012

  b.

  Sending

  Data

  Result

  Edited Picture

  PIC 9(6)

  412345

  $12345

  PIC $(6) or $$$$$$

  000123

  **$123

  000001

  ****$1

  000000

  ******

  c.

  Sending

  Data

  Result

  Edited Picture

  PIC 9(6)V99

  012345

  $123.00

  PIC $ZZ9.00

  000123

  $**1.00

  000025

  $**0.00

  000000

  $**0.00

  d.

  Sending

  Data

  Result

  Edited Picture

  PIC S9(4)

  1234

  +1234

  PIC +(5) or +++++

  -0012

  **-12

  0004

  ***+4

 

‹ Prev