Michael Coughlan

Home > Other > Michael Coughlan > Page 8


  01 ThreeLetterAcronym PIC AAA VALUE "DNA".

  X

  Indicates an occurrence of any character from the character set at the corresponding position in the

  picture:

  01 Salutation PIC XXX VALUE "Mr.".

  9

  Indicates the occurrence of a digit at the corresponding position in the picture:

  01 CardinalValue PIC 9(4) VALUE 1234.

  V

  Indicates the position of the decimal point in a numeric value. It is often referred to as the assumed decimal point because it is not part of the value but is rather information about the value: 01 TotalSales PIC 9(5)V99 VALUE ZEROS.

  S

  Indicates the presence of a sign, and can only appear at the beginning of a picture:

  01 IntegerValue PIC S9(4) VALUE -1234.

  ■ Note there are many more picture symbols than those listed in table 3-2. Most of the remaining symbols will be introduced when you explore edited pictures in Chapter 9.

  PICTURE Clause Notes

  Although the word PICTURE can be used when defining a PICTURE clause, it is normal to use the abbreviation PIC. The recurring symbols in a PICTURE clause can become difficult to count, especially at a glance, so it is normal practice to specify recurring symbols by using a repeat factor inside brackets. For instance:

  PIC 9(8) is equivalent to PICTURE 99999999.

  PIC 9(7)V99 is equivalent to PIC 9999999V99.

  PICTURE X(15) is equivalent to PIC XXXXXXXXXXXXXXX.

  PIC S9(5)V9(4) is equivalent to PIC S99999V9999.

  PICTURE 9(18) is equivalent to PIC 999999999999999999.

  The repeat factor in brackets is normally used for any picture string with more than three symbols. For instance, a three-digit data item might be described as PIC 999, but a four-digit item would be PIC 9(4).

  Numeric values can have a maximum of 18 digits, whereas the limit on string values (PIC X) is usually system dependent.

  ■ ISO 2002 in the 2002 standard, the maximum number of digits in a numeric literal or PICTURE clause was increased from 18 digits to 31 digits.

  41

  Chapter 3 ■ Data DeClaration in CoBol

  Example Declarations

  In typed languages, because the memory required is defined by a variable’s data type, a picture of what is happening to the data in the memory is not useful. In COBOL, however, knowing what is happening to the data in memory is very important for a proper understanding of how data declaration and data movement work. For this reason, many of the examples in this book are accompanied by an illustration that attempts to show what is happening to the data.

  Consider the examples that follow. A simplified version of what is happening in memory is shown in Example 3-1.

  Later examples use more granular illustrations that show each character of storage. One thing you can already note from even this simple example is that although TaxRate is given an initial value of .35, what is actually in memory is 35. The V in the PICTURE clause tells COBOL to treat this item as if it had a decimal point in the leftmost character position. Example 3-1 also reveals that the values in alphanumeric data items are left aligned and space filled (the example shows the space character as *), whereas numeric data items seem to be right aligned (they aren’t—see the “MOVE Rules” section later in this chapter) and zero filled.

  Example 3-1. Data Items and Memory Representation

  WORKING-STORAGE SECTION

  Num1

  Num2 TaxRate

  CustomerName

  000

  015

  35

  Mike***********

  DATA DIVISION.

  WORKING-STORAGE SECTION.

  01 Num1 PIC 999 VALUE ZEROS.

  01 Num2 PIC 999 VALUE 15.

  01 TaxRate PIC V99 VALUE .35.

  01 CustomerName PIC X(15) VALUE "Mike".

  Assignment in COBOL

  In typed languages, the assignment operation is simple because assignment is only allowed between items with compatible types. The simplicity of assignment in these languages is achieved at the cost of having a large number of data types.

  In COBOL, there are only three basic data types:

  • Alphabetic (PIC A)

  • Alphanumeric (PIC X)

  • Numeric (PIC 9)

  But this simplicity is achieved at the cost of having a complex assignment statement.

  The MOVE Verb

  Assignment in COBOL is achieved using the MOVE verb. The COMPUTE verb, which assigns the result of an arithmetic expression to a data item, should never be used to assign the value of one item to another. Similarly, the SET verb, which can be used to set a condition name to TRUE or to change the value in a table index, should only be used for these specialized purposes. All ordinary assignments should use MOVE.

  42

  Chapter 3 ■ Data DeClaration in CoBol

  MOVE Syntax

  The MOVE metalanguage makes the verb seem simple but its operation is complicated by a set of governing rules. The metalanguage for MOVE is as follows:

  MOVE Source$#il TO Destination$#i…

  MOVE copies data from the source identifier (or literal) to one or more destination identifiers. The source and destination identifiers can be group or elementary data items.

  In most programming languages, the data movement in an assignment statement is from right to left. That is, data is copied from the source item on the right to the destination item on the left (for example, Dest := Source Modula-2

  or Dest = Source Java). COBOL does things differently. In COBOL, the MOVE verb copies data from the source item on the left to the destination item(s) on the right. Almost all the COBOL verbs conform to this pattern of data movement. The COMPUTE verb, which has its destination data item on the left, is the one exception.

  MOVE Rules

  The major rules for the MOVE verb are given here. As with all the other verbs, you need to consult your COBOL manual for the more esoteric rules:

  • The source and destination identifiers can be either elementary or group data items.

  • When data is copied into a destination item, the contents of the destination item are

  completely replaced. The contents of the source item are undisturbed.

  • If the number of characters in the source item is too few to fill the destination item, the rest of the destination item is filled with zeros or spaces.

  • If the number of characters in the source item is too many to fit in the destination item, the

  characters that cannot fit are lost. This is known as truncation.

  • When the destination item is alphanumeric or alphabetic (PIC X or A), data is copied into the

  destination area from left to right, with space-filling or truncation on the right.

  • When the destination item is numeric or edited numeric, data is aligned along the decimal

  point with zero-filling or truncation as necessary.

  • When the decimal point is not explicitly specified in either the source or destination item(s),

  the item is treated as if it had an assumed decimal point immediately after its rightmost

  character.

  MOVE Combinations

  Although COBOL is much less restrictive in this respect than many other languages, certain combinations of sending and receiving data types are not permitted (even by COBOL) and will be rejected by the compiler. The valid and invalid MOVE combinations are shown in Figure 3-1.

  43

  Chapter 3 ■ Data DeClaration in CoBol

  Figure 3-1. Valid and invalid MOVE combinations

  Source: J.M. Triance, Structured COBOL Reference Summary, National Computing Centre Limited, N.C.C Publications, 1984, page 48.

  MOVE Examples

  Having a dusty set of rules is all very well, but the operation of the MOVE verb can only be appreciated by examining some examples. The examples in this section only feature elementary data items. Things get a lot more exciting whe
n MOVE is used with a group item; you will see some examples of group item moves at the end of the chapter.

  Alphanumeric MOVEs

  Remember the following rule for alphanumeric MOVEs: when the destination item is alphanumeric or alphabetic (PIC X or A), data is copied into the destination area from left to right with space-filling or truncation on the right.

  In Example 3-2, the data item Surname is described as having sufficient storage for eight alphanumeric characters.

  It has been assigned an initial value of "COUGHLAN".

  Example 3-2. Alphanumeric Moves with Truncation and Space Filling

  01 Surname PIC X(8) VALUE "COUGHLAN".

  Surname

  MOVE "SMITH" TO Surname

  C

  O

  U

  G

  H

  L

  A

  N

  MOVE "FITZWILLIAM" TO Surname

  S

  M

  I

  T

  H

  *

  *

  *

  F

  I

  T

  Z

  W

  I

  L

  L

  When the first move is executed, "SMITH" is copied into Surname from left to right. Because "SMITH" has too few characters to fill Surname, the rest of the data item is filled with spaces.

  The second move copies "FITZWILLIAM" into Surname from left to right. Because the literal is too large to fit into Surname , the last three letters (IAM)are truncated.

  44

  Chapter 3 ■ Data DeClaration in CoBol

  Numeric MOVEs

  Remember the following rule for numeric MOVEs: when the destination item is numeric or edited numeric, data is aligned along the decimal point with zero-filling or truncation as necessary. An edited numeric data item is one that contains symbols such as $ and , and . that format data for output. They are not numeric items, and they can’t be used in calculations (except as the receiving field), but they do obey the decimal-point alignment and zero-filling rules. Edited numeric data items are discussed in Chapter 9.

  When the decimal point is not explicitly specified in either the source or destination item(s), the item is treated as if it had an assumed decimal point immediately after its rightmost character.

  Example Set 1

  In Example 3-3, SalePrice is a data item described as PIC 9(4)V99: that is, a decimal number with four digits before the decimal point and two after it. For each MOVE statement, a diagram showing the contents of the SalePrice data item is given. Each diagram shows the actual data moved in black, the filled zeros in grey, and the truncated digits outside the memory area. The position of the assumed decimal point is indicated with an arrow.

  When the figurative constant ZEROS (or ZERO or ZEROES) is moved to SalePrice , the data item is filled with zeros.

  When the numeric literal 25.5 is moved to SalePrice, there is alignment along the decimal point of the literal and the assumed decimal point in SalePrice, with the result being zero-filling on both the left and right sides.

  When 7.553 is moved to SalePrice, there is alignment of the decimal points, with the result being zero-filling on the left and truncation of the digit 3 on the right. In the diagram the truncated digit is shown outside the memory area.

  When 93425.158 is moved to SalePrice , there is alignment of the decimal points, with the result that the most significant digit is truncated on the left and the least significant on the right.

  The literal value 128 contains no decimal point, so it is treated as if it had a decimal point in the rightmost position. This decimal point is aligned with assumed decimal point in SalePrice, with the result that there is zero filling on the left and right.

  Example 3-3. Numeric MOVEs with Alignment Along the Decimal Point, Truncation, and Zero-Filling 01 SalePrice PIC 9(4)V99.

  SalePrice

  MOVE ZEROS TO SalePrice

  0

  0

  0

  0

  0

  0

  MOVE 25.5 TO SalePrice

  0

  0

  2

  5

  5

  0

  MOVE 7.553 TO SalePrice

  0

  0

  0

  7

  5

  5

  3

  MOVE 93425.158 TO SalePrice

  9

  3

  4

  2

  5

  1

  5

  8

  MOVE 128 TO SalePrice

  0

  1

  2

  8

  0

  0

  

  •

  Inadvertent truncation is obviously not desirable; but unfortunately, for MOVE operations at least, there is no protection against it. It is up to you to ensure that the data item is large enough to take the data moved into it.

  Inadvertent truncation is much more likely when calculations are involved. When a number of values are multiplied together, you might not realize that in some cases the result will be too large for the receiving data item. For computations, you can protect against inadvertent truncation by means of the ON SIZE ERROR clause. When this clause is used, it acts like a specialized exception handler. Chapter 4 discusses ON SIZE ERROR when you examine the operation of the COBOL arithmetic verbs.

  45

  Chapter 3 ■ Data DeClaration in CoBol

  Example Set 2

  In Example 3-4, NumOfEmployees is described as a cardinal number capable of holding a value between 0 and 999.

  Salary is a decimal number data item with four digits before the decimal point and two after it. CountyName is an alphanumeric data item nine characters long.

  The literal value 6745 has no decimal point, so it is treated as if the number has a decimal point in the rightmost position (6745.). NumOfEmployees also contains no explicit decimal point, so it is treated as if the data item has an assumed decimal point specified in the rightmost position:

  01 NumOfEmployees PIC 999V

  When the literal 6745 is moved to NumOfEmployees, there is alignment along these decimal points. The result is truncation of the most significant digit.

  When NumOfEmployees (treated as if defined as 01 NumOfEmployees PIC 999V) is moved to Salary, which does

  have an explicit decimal point, there is alignment along the decimal points, with the result being zero-filling on both the left and right.

  When the literal value 12.4 is moved to NumOfEmployees (treated as if defined as 01 NumOfEmployees PIC 999V), there is alignment along the decimal points with truncation of the digit 4 on the right and zero-filling on the left.

  When the literal “Galway” is moved to CountyName, the data movement starts filling the data item from the left.

  When the value does not entirely fill the data item, the remaining character positions are space-filled.

  When the figurative constant ALL and its associated character literal are moved to a data item, the data item is entirely filled with the character specified. In this example, the character specified after ALL is the hyphen, so CountyName is filled with hyphens.

  Example 3-4. Numeric and Alphanumeric MOVEs

  01 NumOfEmployees PIC 999.

  NumOfEmployees

  MOVE 12.4 TO NumOfEmployees

  0

  1

  2

  4

  0

  MOVE 6745 TO NumOfEmployees

  6

  7

  4

  5

  0

  0

  

  •

  01 Salary PIC 9999V99.

  Salary

  MOVE NumOfEmployees TO Salary

  0

  7

  4

  5

  0

  0

  

  •

  01 CountyName PIC X(9
).

  CountyName

  MOVE "GALWAY" TO CountyName

  G

  A

  L

  W A

  Y

  *

  *

  *

  MOVE ALL "@" TO CountyName

  @

  @ @ @ @ @ @ @ @

  Structured Data

  In COBOL, the term elementary item describes an ordinary data item or variable. An elementary item is a data item that is atomic: it has not been further subdivided. Every elementary item must have a PICTURE clause. The PICTURE

  clause specifies the type and size of the storage required for the data item.

  46

  Chapter 3 ■ Data DeClaration in CoBol

  Group Data Items

  Sometimes when you are manipulating data it is convenient to treat a collection of elementary items as a single group. For instance, you might want to group the data items Forename, MiddleInitial, and Surname as the group EmployeeName. Alternatively, you might want to group the YearOfBirth, MonthOfBirth, DayOfBirth data items as the group DateOfBirth. In addition, you might want to collect both these group items and some elementary items in an employee record description.

  In COBOL, you can easily create groups like these using group items. A group item in COBOL is a data item that is a collection of elementary and/or group data items. It is a heterogeneous data structure. In languages like Pascal and Modula-2, group items are referred to as records. In C and C++, they are called structs. Java has no real equivalent .

  The constituent parts of a group item may be elementary items or other group items. But ultimately, every

  group item must be defined in terms of its subordinate elementary items. Because a group item is ultimately defined in terms of elementary items, it cannot have a PICTURE clause, and its size is the sum of the sizes of its subordinate elementary items. A group item is simply a convenient name that you give to a collection of (ultimately) elementary items. Using that name, you can manipulate the collection.

  In a group item, the hierarchical relationship between the various subordinate items of the group is expressed using level numbers. The higher the level number, the lower the item is in the hierarchy and the more atomic it is.

  If a group item is the highest item in a data hierarchy, it is referred to as a record and uses the level number 01.

 

‹ Prev