[This chapter] [TOC] [Tutorial]
77 CUSTOMER-NAME PIC X(30).The variable is called CUSTOMER-NAME and is an alphanumeric field containing 30 characters. Note the hyphen in the name: COBOL does not usually allow you to use an underline "_". An alphanumeric variable will always contain the number of characters it has been defined with, i.e. not the actual length of the variable as in Pascal or C++. Level number 77 here means that the variable is not connected to other variables.
[This chapter] [TOC] [Tutorial]
66 Used when redefining the data type of a prior variable.
77 Variable.
78 A Micro Focus speciality. The same as 77, but with the added element that the field is a named constant which cannot alter its value while the program is being run.
88 Used to define symbolic conditions. More on this later.
[This chapter] [TOC] [Tutorial]
X(n)An example:
An alphanumeric field with space for n characters.
S9(n1)V9(n2)
A calculation field with sign, space for n1 characters before the decimal point and n2 characters after. Note that the field does not contain a decimal point. The decimal point is represented by the "V".
--.---.--9,99
--.---.--9vbb99
A numeric edited field which fills 13 print positions.
The "V" represents the position of the decimal point here too (for example, on giro payment slips). "b" is used to indicate space characters in the output. If the figure is negative there will be a minus sign in front of the first significant digit; leading zeros are replaced by blanks. Commas for thousands, etc., will only be displayed if the figure is larger than (+/-)999 or (+/-)999999.
After this PICTURE mask, it is possible to add an internal format, e.g. DISPLAY or COMP. Alphanumeric fields or numeric-edited fields always use the DISPLAY format.
DATA DIVISION. WORKING-STORAGE SECTION. 77 ENTERED-VALUE PIC 999. 77 INTERNAL-VALUE PIC S999 COMP. .. PROCEDURE DIVISION. ... MOVE ENTERED-VALUE TO INTERNAL-VALUE. ...An explanation of the example:
[This chapter] [TOC] [Tutorial]
ZERO(S)(ES)One or more zeroes, either as an ASCII string (or EBCDIC) or a numerical zero - depending how it is used.SPACE(S)
A series of blank spaces.HIGH-VALUE(S)The highest value in the code page.LOW-VALUE(S)The lowest value in the code page.QUOTE(S)Inverted commas. Used so as not to confuse the '"' sign in the syntax.ALL literalSame as SPACE, but here the programmer decides which character is to be used as SPACE.
77 ADDRESS PIC X(25) VALUE SPACE.The variable ADDRESS may contain 25 characters which are immediately initiated as being blank characters.
77 INTERNAL-VALUE PIC 999 COMP. ... MOVE ZERO TO INTERNAL-VALUE.The variable INTERNAL-VALUE is a binary field that may contain values within the interval 0-999. The compiler ensures that it creates the optimal constant. Another alternative is:
MOVE "0" TO INTERNAL-VALUEbut this creates a character constant with the value "0", which therefore has to be converted to a binary format every time the instruction is carried out.
[This chapter] [TOC] [Tutorial]
000000* A sample program showing the use of some different variables
identification division.
* ------------ identifying the program.
program-id. variables.
author. kik.
environment division.
* ------------ program environment
configuration section.
special-names.
console is crt
decimal-point is comma.
* ------------ Variables
data division.
working-storage section.
* Level number
* | Name
* | | Picture mask
* | | | Internal format
* | | | | Initial value
* | | | | |
77 enter-field pic 9999.
77 calc-field pic s9999 comp value zero.
77 customer-name pic x(30) value space.
77 price pic s9(6)v99 comp value zero.
77 ed-price pic ----.--9,99.
77 bar pic x(80) value all "--*".
procedure division.
* ------------ The instructions
main section.
display space.
display bar line 3 position 1.
display "Enter a figure :" line 5 position 10.
accept enter-field line 5 position 34.
display "Enter a customer name :" line 6 position 10.
accept customer-name line 6 position 34.
if enter-field numeric then
move enter-field to calc-field.
add calc-field to price.
move price to ed-price.
display "price : " line 7 position 10.
display ed-price line 7.
exit program.
The above program creates this screen during execution:---*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--
Enter a figure : 1000
Enter a customer name : Kim Kjærsulf
price : 1.000,00
[This chapter] [TOC] [Tutorial]
01 cprnumber pic x(10). 01 cpr-data redefines cprnumber. 03 day pic 99. 03 mth pic 99. 03 yr pic 99. 03 cpr-no. 05 lbno pic 999. 05 check pic 9.Here it is possible to refer to a CPR number (Danish civil registration number) as a text string, to each element in the number as figures, and finally, to the last four characters as text.
[This chapter] [TOC] [Tutorial]