Moving and Editing pages

4.2 Moving and Editing Data

Care must be taken when moving (using the MOVE verb) data to an item. In other languages, such as Pascal, when to assign a value to XYZ (defined at the beginning of the program as an integer for example), then that's it. In COBOL, you have to be sure that the value you are moving to XYZ item is not bigger than the defined size. If you moved 1234 to a PIC 999 defined item, then if you were to examine XYZ it would contain 234. For numeric data, the digits are truncated from the left. If item ABC were defined as PIC XXX and you moved "abcd" into it, on examination you would find it contained "abc". For alpha-numeric data characters are truncated from the right.

Conversely, moving data that is smaller than the PIC definition has certain effects. If 2 was moved to item XYZ above, then the number 2 is written to the right-most position and the leading positions are zero-filled (see figure below). Likewise, moving "A" to ABC above, the letter "A" would be written to the left-most position with the trialing positions being space-filled.

So what about data with decimal places?
To use decimal places, the letter 'V' is used in the PIC description:

      01 COST-OF-ITEM PIC 9(4)V99.

Here the item COST-OF-ITEM can contain a number that has two decimal places. 'V' is called an implied decimal place in that the 'V' itself is not an area of memory, i.e. the above PIC description will hold 6 digits - 4 before the decimal point and two after. The computer will align the number being moved into the item around the decimal point. See the examples below:

number going into COST-OF-ITEM   contents of COST-OF-ITEM  [PIC 9(4)V99]
     1234.56                      1234.56
     1234                         1234.00  (zero-filled)
        1                         0001.00  (zero-filled)
        0.1                       0000.10  (zero-filled)
   654321.12                      4321.12  (digits are truncated)
   654321                         4321.00  (digits are truncated)
     1234.567                     1234.56  (figure is NOT rounded up to 1234.57)

If you were to display COST-OF-ITEM it would appear as 123456 since the decimal point is assumed, not actual. For the purposes of printing or displaying a number you would need to actually show where decimal point is. You may also wish to avoid having a long string of zeros in front of a number and have spaces used instead. So the number would first have to be moved into an outputing item...well have a look at the small program below (don't worry too much about any of the commands used in the procedure division, although they're pretty self explanatory):


  • the VDU will then diplay:
    £ 22.80 in total
  • The program gives values to COST-OF-ITEM and NUMBER-OF-ITEMS (lines 180 & 190) and multiples them to give TOTAL-COST (line 200).
  • This result of this calculation is then moved to TOTAL-COST-OUT (line 210).
  • Line 140 has a PIC description 'ZZZZ9.99'. The Z means 'zero-supression' where spaces are added instead of zeros. This means that you would not be allowed to perform arithemic functions on TOTAL-COST-OUT since it now contains alphanumeric data (i.e. spaces). Also, an actual decimal point (i.e. a full stop) is used in place of the 'V'
  • You could actually write in line 200: COMPUTE TOTAL-COST-OUT = COST-OF-ITEMS * NUMBER-OF-ITEMS. Here the result of the calculation is put straight into TOTAL-COST-OUT, but no further calculations could be performed on the result.

    You'll notice that there is a gap (of 2 spaces) between the '£' sign and the actual number in the displayed output. To avoid this when using '£' or '$' signs (well COBOL is a business language), you can zero-suppress as follows:

  •      000140 01 TOTAL-COST-OUT  PIC ££££9V99.

    the VDU will then display:
    £22.80 in total


    If you want nothing in a field when printing a value that is zero then use BLANK WHEN ZERO:

         000140 01 PRINT-VALUE  PIC Z(5)9V99 BLANK WHEN ZERO.

    the VDU will then display:   nothing...


    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp More Data Editing

    Signed data needs to be able to indicate whether it is a positive or negative number. An item may have a definition:

    &nbsp&nbsp&nbsp&nbsp 01 DATA-VALUE-1 PIC S999.

    'S' indicates that the data is signed and so allows for negative values to be stored. If data is being entered from a keyboard say, as -201, into DATA-ITEM-2, the computer needs to be told that the first character is a sign:

    &nbsp&nbsp&nbsp&nbsp 01 DATA-VALUE-2 PIC S999 SIGN LEADING SEPARATE.

    This would be suitable for a item into which "-201" can be entered. Here 'SIGN LEADING SEPARATE' indicates that a + or - occurs immediately before the number (you can also have 'SIGN TRAILING SEPARATE'). The important feature is the 'S' prior to the 999 (irrespective of leading or trailing signs).

    For output, the sign can be maniputed to show signs and zero-suppress using a 'floating sign'. Look at the following examples:

    Sending field Receiving field
    &nbsp Description (PIC)&nbsp &nbsp Contents&nbsp  &nbsp Description (PIC)&nbsp &nbsp Contents&nbsp 
    S999-21 S999-021
    S999-21 --99-21
    S999+21 --9921
    S999-21 ++99-21
    S999+21 ++99+21
    S999 SIGN LEADING SEPARATE-347 999+347-
    S999 SIGN TRAILING SEPARATE347- -999-347

    The last two examples in the table show how the sign can be moved to the other end of the number when SIGN LEADING/TRAILING SEPARATE is used.

    Some characters can be inserted into numbers, these being SPACE, SOLIDUS, ZERO (using 'B' '/' and '0' respectively):

    Sending field Receiving field
    &nbsp Description (PIC)&nbsp &nbsp Contents&nbsp  &nbsp Description (PIC)&nbsp &nbsp Contents&nbsp 
    9999912345 99B9B9912 3 45
    9999912345 99090991203045
    999999170568 99/99/9917/05/68

    Adding a solidus can be useful for printing the date (which can be obtained directly from the computer in the form of yymmdd [you have to switch the order around first]). I can only assume that adding zeros to a number is for fraudulent purposes.


    &nbsp Redefining Data

    It is sometimes useful to be able to have data that can be defined as either numeric or alphanumeric. This is done by redefining the data. One way is implicit redefinition:

        01 DATA-ITEM-X.
             03 DATA-ITEM-1  PIC 99.

    Although DATA-ITEM-X and DATA-ITEM-1 refer to the same area of memory storage, the level 03 item is numeric. However, group items are always alphanumeric and as a result, if you moved the number 25 into DATA-ITEM-1, you could use DATA-ITEM-X as an alphanumeric item containing the literal "25".

    Explicit redefinition uses the verb REDEFINES so that you could do this:

        01 DATA-ITEM-X.
             03 DATA-ITEM-1  PIC 99.

    REDEFINES cannot be used for level 01 items and can only redefine items on the same level.

    Another use for REDEFINES is to offer an alternative PIC desciption for the same data group:

        01 DATE-TODAY.
              03 UK-DATE.
                   05 UK-DAY      PIC 99.
                   05 UK-MONTH    PIC 99.
                   05 UK-YEAR     PIC 99.
                   05 JUL-YEAR    PIC 99.
                   05 JUL-DAY     PIC 999.

    • UK date format is ddmmyy while Julian date format is yyddd (i.e. nth day of the year)
    • You could move (depending on the type of date given) the date into either UK-DATE or JULIAN-DATE and later in the program call the date using DATE-TODAY
    • JULIAN-DATE has one less 9 than UK-DATE. The computer will simply space-fill the unused byte.