Professional Documents
Culture Documents
Course Details
No.
Name
COBOL
Course Details
Pre-Requisite
None
Target
COBOL
Audience
Mode of
Training
Evaluation
Criteria
Course Details
Module
No.
Topic
Time(hrs)
1.
Introduction to COBOL
2.
COBOL Basics 1
3.
COBOL Basics 2
4.
5.
6.
Conditions
7.
COPY Verb
8.
The PERFORM
9.
Tables and
PERFORM..VARYING
10.
Advanced Tables
Course Details
Module
No.
Topic
Time(hrs)
11.
Searching Tables
12.
13.
14.
15.
16.
Relative Files
17.
Indexed Files
18.
String
19.
Unstring
20
Sort
Introduction to
COBOL
COBOL
COBOL idiosyncracies
When they are not underlined the used for readability only and are
optional. If used they must be spelt correctly.
The ellipsis symbol ... indicates that the preceding syntax element
may be repeated at the programmers discretion.
Program
Program
Divisions
Section(s)
Paragraph(s)
Sentence(s)
Statement(s)
DIVISION.
ENVIRONMENT DIVISION.
DATA DIVISION.
PROCEDURE DIVISION.
NNOTE
OTE
The
Thekeyword
keyword
DIVISION
DIVISIONand
andaa
full-stop
full-stopisisused
usedinin
every
everycase.
case.
IDENTIFICATION DIVISION.
IDENTIFICATION
IDENTIFICATION DIVISION.
DIVISION.
PROGRAM-ID.
PROGRAM-ID. FirstProgram.
FirstProgram.
AUTHOR.
AUTHOR. Hari.
Hari.
WS entries.
IDENTIFICATION
IDENTIFICATION DIVISION.
DIVISION.
PROGRAM-ID.
PROGRAM-ID. FirstProgram.
FirstProgram.
AUTHOR.
Hari.
AUTHOR. Hari.
DATA
DATA DIVISION.
DIVISION.
WORKING-STORAGE
WORKING-STORAGE SECTION.
SECTION.
01
PIC
01 Num1
Num1
PIC 99 VALUE
VALUE ZEROS.
ZEROS.
01
Num2
PIC
9
VALUE
ZEROS.
01 Num2
PIC 9 VALUE ZEROS.
01
PIC
01 Result
Result
PIC 99
99 VALUE
VALUE ZEROS.
ZEROS.
Sections
Paragraphs
IDENTIFICATION
IDENTIFICATION DIVISION.
DIVISION.
PROGRAM-ID.
SmallestProgram.
PROGRAM-ID. SmallestProgram.
PROCEDURE
PROCEDURE DIVISION.
DIVISION.
DisplayPrompt.
DisplayPrompt.
DISPLAY
DISPLAY "I
"I did
did it".
it".
GOBACK.
GOBACK.
COBOL Basics 1
Columns 7
Column 8-11
Column 12-72
Column 73 on
Name Construction.
They must contain at least one character and not more than 30
characters.
Describing DATA.
There are basically three kinds of data used in COBOL
programs;
Variables.
Literals.
Figurative Constants.
Unlike other programming languages, COBOL does
not support user defined constants.
Data-Names / Variables
Using Variables
01 StudentName
StudentName
Using Variables
01 StudentName
StudentName
O H
Using Variables
01 StudentName
StudentName
O H
My name is JOHN
The type usually determines the range of values the data item
can store.
From the type of the item the compiler can establish how much
memory to set aside for storing its values.
examples
PICTURE 999
a three digit (+ive only) integer
PICTURE S999 a three digit (+ive/-ive) integer
PICTURE XXXX
a four character text item or
string
PICTURE 99V99
a +ive real in the range 0 to
99.99
PICTURE S9V9
a +ive/-ive real in the range ?
If
Numeric
The
DATA DIVISION.
WORKING-STORAGE SECTION.
01 Num1
PIC 999 VALUE ZEROS.
01 VatRate
PIC V99 VALUE .18.
01 StudentName
PIC X(10)VALUE SPACES.
DATA
Num1
Num1 VatRate
VatRate
000
000
.18
.18
StudentName
StudentName
COBOL Literals.
Figurative Constants
==
==
00
QUOTE
QUOTEor
orQUOTES
QUOTES
HIGH-VALUE
HIGH-VALUEor
orHIGH-VALUES
HIGH-VALUES
==
""
==
Max
MaxValue
Value
LOW-VALUE
LOW-VALUEor
orLOW-VALUES
LOW-VALUES
ALL
ALLliteral
literal
==
Min
MinValue
Value
==
Fill
FillWith
WithLiteral
Literal
PIC
PIC 9(5)V99
9(5)V99 VALUE
VALUE 13.5.
13.5.
TO
TO GrossPay.
GrossPay.
GrossPay
0 0 0 1 3 5 0
01
01 StudentName
StudentName
PIC
PIC X(10)
X(10) VALUE
VALUE "MIKE".
"MIKE".
MOVE
MOVE ALL
ALL "-"
"-" TO
TO StudentName.
StudentName.
StudentName
M I K E
PIC
PIC 9(5)V99
9(5)V99 VALUE
VALUE 13.5.
13.5.
TO
TO GrossPay.
GrossPay.
GrossPay
0 0 0 0 0 0 0
01
01 StudentName
StudentName
PIC
PIC X(10)
X(10) VALUE
VALUE "MIKE".
"MIKE".
MOVE
MOVE ALL
ALL "-"
"-" TO
TO StudentName.
StudentName.
StudentName
- - - - - - - - - -
COBOL Basics
2
Group Items/Records
WORKING-STORAGE
WORKING-STORAGE SECTION.
SECTION.
01
StudentDetails
PIC
01 StudentDetails
PIC X(26).
X(26).
StudentDetails
H E N N E S S Y R M 9 2 3 0 1 6 5 L M 5 1 0 5 5 0 F
Group Items/Records
WORKING-STORAGE
WORKING-STORAGE SECTION.
SECTION.
01
StudentDetails.
01 StudentDetails.
02
PIC
02 StudentName
StudentName
PIC X(10).
X(10).
02
StudentId
PIC
9(7).
02 StudentId
PIC 9(7).
02
PIC
02 CourseCode
CourseCode
PIC X(4).
X(4).
02
Grant
PIC
9(4).
02 Grant
PIC 9(4).
02
Gender
PIC
02 Gender
PIC X.
X.
StudentDetails
H EN N E S S Y RM 9 2 3 0 1 6 5 L M 5 1 0 5 5 0 F
StudentName
StudentId
CourseCode Grant
Gender
Group Items/Records
WORKING-STORAGE
WORKING-STORAGE SECTION.
SECTION.
01
StudentDetails.
01 StudentDetails.
02
02 StudentName.
StudentName.
03
PIC
03 Surname
Surname
PIC X(8).
X(8).
03
PIC
03 Initials
Initials
PIC XX.
XX.
02
StudentId
PIC
9(7).
02 StudentId
PIC 9(7).
02
CourseCode
PIC
02 CourseCode
PIC X(4).
X(4).
02
PIC
02 Grant
Grant
PIC 9(4).
9(4).
02
Gender
PIC
X.
02 Gender
PIC X.
StudentDetails
H EN N E S S Y RM 9 2 3 0 1 6 5 L M 5 1 0 5 5 0 F
StudentName
Surname
StudentId
Initials
CourseCode Grant
Gender
01
01 StudentDetails.
StudentDetails.
02
02 StudentName.
StudentName.
03
03 Surname
Surname
03
Initials
03
Initials
02
StudentId
02 CourseCode
StudentId
02
02 Grant
CourseCode
02
02 Gender
Grant
02
02 Gender
PIC
PIC X(8).
X(8).
PIC
XX.
PIC
XX.
PIC
9(7).
PIC X(4).
9(7).
PIC
PIC 9(4).
X(4).
PIC
PIC X.
9(4).
PIC
PIC X.
01
01 StudentDetails.
StudentDetails.
05
05 StudentName.
StudentName.
10
Surname
10
Surname
10
Initials
10 Initials
05
StudentId
05 CourseCode
StudentId
05
05 Grant
CourseCode
05
05 Gender
Grant
05
05 Gender
PIC
X(8).
PIC
X(8).
PIC
XX.
PIC 9(7).
XX.
PIC
PIC X(4).
9(7).
PIC
PIC 9(4).
X(4).
PIC
PIC X.
9(4).
PIC
PIC X.
The term elementary item is used to describe data items which are
atomic; that is, not further subdivided.
Assignment in COBOL
Alphabetic (PIC A)
Alphanumeric (PIC X)
Numeric (PIC 9)
MOVE
Literal
TO Identifier ...
MOVEing Data
MOVE
MOVE RYAN
RYAN TO
TO Surname.
Surname.
MOVE
MOVE FITZPATRICK
FITZPATRICK TO
TO Surname.
Surname.
01 Surname
PIC X(8).
C O U G H L A N
MOVEing Data
MOVE
MOVE RYAN
RYAN TO
TO Surname.
Surname.
MOVE
MOVE FITZPATRICK
FITZPATRICK TO
TO Surname.
Surname.
01 Surname
R Y A N
PIC X(8).
MOVEing Data
MOVE
MOVE RYAN
RYAN TO
TO Surname.
Surname.
MOVE
MOVE FITZPATRICK
FITZPATRICK TO
TO Surname.
Surname.
01 Surname
PIC X(8).
F I T Z P A T R I C K
01 GrossPay
PIC 9(4)V99.
GrossPay
0 0 0 0 0 0
GrossPay
0 0 1 2 4 0
GrossPay
0 1 2 3 4 5 6
GrossPay
1 2 3 4 5 7 5 7
01 CountyPop
01 Price
PIC 999.
PIC 999V99.
CountyPop
1 2 3 4
CountyPop
0 1 2 4
Price
1 5 4 0 0
Price
3 5 5 2 7 5
Legal MOVEs
Certain combinations of sending and receiving data
types are not permitted (even by COBOL).
DISPLAY
Literal
Identifier
...
Literal
DAY
TIME
01
CurrentDate
01
CurrentDate
* YYMMDD
PIC
PIC 9(6).
9(6).
01
DayOfYear
01
DayOfYear
* YYDDD
PIC
PIC 9(5).
9(5).
01
Day0fWeek
01
Day0fWeek
* D (1=Monday)
PIC
PIC 9.
9.
01
CurrentTime
01
CurrentTime
* HHMMSSss
s = S/100
PIC
PIC 9(8).
9(8).
* YYMMDD
* YYDDD
* D (1=Monday)
* HHMMSSss
s = S/100
$ SET SOURCEFORMAT"FREE"
$ SET SOURCEFORMAT"FREE"
IDENTIFICATION
DIVISION.
IDENTIFICATION
DIVISION.
PROGRAM-ID. AcceptAndDisplay.
PROGRAM-ID.
AcceptAndDisplay.
AUTHOR. Michael Coughlan.
AUTHOR. Michael Coughlan.
DATA DIVISION.
DATA DIVISION. SECTION.
WORKING-STORAGE
WORKING-STORAGE
SECTION.
01
StudentDetails.
0102
StudentDetails.
StudentName.
02 03
StudentName.
Surname
PIC X(8).
03Initials
Surname
PICXX.
X(8).
03
PIC
03
Initials
PIC
XX.
02 StudentId
PIC 9(7).
02
StudentId
PIC
9(7).
02 CourseCode
PIC X(4).
02 Grant
CourseCode
PIC9(4).
X(4).
02
PIC
02
Grant
PIC
9(4).
02 Gender
PIC X.
02 Gender
PIC X.
01 CurrentDate.
0102
CurrentDate.
CurrentYear
PIC 99.
02 CurrentMonth
CurrentYear
PIC99.
99.
02
PIC
02
CurrentMonth
PIC
99.
02 CurrentDay
PIC 99.
02 CurrentDay
PIC 99.
01 DayOfYear.
0102
DayOfYear.
FILLER
PIC 99.
02 YearDay
FILLER
PIC9(3).
99.
02
PIC
02 YearDay
PIC 9(3).
01 CurrentTime.
0102
CurrentTime.
CurrentHour
PIC 99.
02 CurrentMinute
CurrentHour
PIC99.
99.
02
PIC
02
CurrentMinute
PIC
99.
02 FILLER
PIC 9(4).
02 FILLER
PIC 9(4).
PROCEDURE DIVISION.
PROCEDURE DIVISION.
Begin.
Begin.
DISPLAY "Enter student details using template below".
DISPLAY"NNNNNNNNNNSSSSSSSCCCCGGGGS
"Enter student details using template below".
DISPLAY
".
DISPLAY
"NNNNNNNNNNSSSSSSSCCCCGGGGS
".
ACCEPT StudentDetails.
ACCEPT
StudentDetails.
ACCEPT CurrentDate FROM DATE.
ACCEPT DayOfYear
CurrentDate
FROM
DATE.
ACCEPT
FROM
DAY.
ACCEPT
DayOfYear
FROM
DAY.
ACCEPT CurrentTime FROM TIME.
ACCEPT "Name
CurrentTime
FROM TIME.
DISPLAY
is ", Initials
SPACE Surname.
DISPLAY"Date
"Nameis
is"",
Initials SPACE
DISPLAY
CurrentDay
SPACE Surname.
CurrentMonth SPACE CurrentYear.
DISPLAY
"Date
is
"
CurrentDay
SPACE CurrentYear.
DISPLAY "Today is day " YearDaySPACE
" of CurrentMonth
the year".
DISPLAY
"Today
is
day
"
YearDay
"
of
the
year".
DISPLAY "The time is " CurrentHour ":" CurrentMinute.
DISPLAY
STOP
RUN."The time is " CurrentHour ":" CurrentMinute.
STOP RUN.
Arithmetic
and
Edited Pictures
VERB
Identifier
GIVING
Identifier
Literal BY
INTO
ROUNDED
123.25
PIC 9(3).
123.25
123.2
123.3
123
123
Actual Result
245.96
1245.9
PIC 9(3).
124
PIC 9(3).
1246
124.45
124.45
3124.45
SIZE ERROR
Yes
Yes
No
Yes
Yes
No
Yes
ADD Examples
Before
Before
After
After
Before
Before
After
After
Before
Before
After
After
Before
Before
After
After
ADD
ADD Cash
Cash TO
TO Total.
Total.
33
1000
1000
3
1003
ADD
ADD Cash,
Cash, 20
20 TO
TO Total,
Total, Wage.
Wage.
33
1000
100
1000
100
3
1023
123
ADD
ADD Cash,
Cash, Total
TotalGIVING
GIVINGResult.
Result.
33 1000
1000
0015
3
1003
1000
0015
ADD
ADDMales
MalesTO
TOFemales
FemalesGIVING
GIVING TotalStudents
TotalStudents
1500
1500
1500
0625
0625
0625
1234
21251234
SUBTRACT Examples
Before
Before
After
After
SUBTRACT
SUBTRACT Tax
Tax FROM
FROMGrossPay,
GrossPay,Total.
Total.
120
4000
9120
120
4000
9120
120
3880
9000
Before
Before
After
After
SUBTRACT
SUBTRACT Tax,
Tax,80
80FROM
FROMTotal.
Total.
100
480
100
480
100
300
Before
Before
After
After
SUBTRACT
SUBTRACT Tax
Tax FROM
FROMGrossPay
GrossPayGIVING
GIVINGNetPay.
NetPay.
750
1000
0012
750
1000
0012
750
1000
0250
Before
Before
After
After
Subs
Subs
15.50
15.50
15.50
100
100
0123.45
0123.45
1550.00
MULTIPLY
Magnitude,
Size.
MULTIPLY10
10BY
BY
Magnitude,
Size.
3550
1250
355
125
355
125
9234.55
Before
Before
After
After
100
Members
Members TotalSubs
TotalSubs
100
92.35
DIVIDE
DIVIDE Total
Total BY
BY Members
MembersGIVING
GIVINGAverage
Average ROUNDED.
ROUNDED.
9234.55
100
1234.56
9234.55
100
1234.56
DIVIDE
ON SIZE ERROR
Identifier
Identifier
Literal
Literal
DIVIDE
BY
ON SIZE ERROR
Before
Before
After
After
DIVIDE
DIVIDE201
201BY
BY10
10GIVING
GIVINGQuotient
Quotient REMAINDER
REMAINDERRemain.
Remain.
209
424
209
424
020
001
The COMPUTE
COMPUTE Identifier [ ROUNDED ] ... = ArithmeticExpression
ON SIZE ERROR
NOT ON SIZE ERROR
Precedence
Precedence Rules.
Rules.
1.
1.
2.
2.
3.
3.
Before
Before
After
After
****
**
//
++
--
==
==
==
==
==
POWER
POWER
MULTIPLY
MULTIPLY
DIVIDE
DIVIDE
ADD
ADD
SUBTRACT
SUBTRACT
NNNN
xx
++
--
Compute
ComputeIrishPrice
IrishPrice==SterlingPrice
SterlingPrice//Rate
Rate ** 100.
100.
1000.50
156.25
87
1000.50
156.25
87
179.59
156.25
87
Edited Pictures.
Editing Types
Suppression and Replacement Editing which suppresses and replaces leading zeros.
Editing Symbols
Edit Symbol
,, B
B 00 //
..
++ -- CR
CR DB
DB $$
++ -- SS
ZZ **
Editing Type
Simple
SimpleInsertion
Insertion
Special
Special Insertion
Insertion
Fixed
FixedInsertion
Insertion
Floating
FloatingInsertion
Insertion
Suppression
Suppressionand
andReplacement
Replacement
Simple Insertion.
Sending
Sending
Picture
Data
Picture
Data
PIC
PIC999999
999999 123456
123456
PIC
000078
PIC9(6)
9(6)
000078
PIC
000078
PIC9(6)
9(6)
000078
PIC
000178
PIC9(6)
9(6)
000178
PIC
002178
PIC9(6)
9(6)
002178
zz
PIC
120183
PIC9(6)
9(6)
120183
PIC
120183
PIC9(6)
9(6)
120183
PIC
001245
PIC9(6)
9(6)
001245
Receiving
Receiving
Picture
Result
Picture
Result
123,456
PIC
999,999
PIC 999,999
000,078
PIC
PIC9(3),9(3)
9(3),9(3)
7
PIC
PICZZZ,ZZZ
ZZZ,ZZZ
8
PIC
****178
PIC***,***
***,***
**2,178
PIC
PIC***,***
***,***
PIC
PIC99B99B99
99B99B99
PIC
PIC99/99/99
99/99/99
PIC
PIC990099
990099
12 01 83
12/01/83
120045
Special Insertion.
Sending
Sending
Picture
Data
Picture
Data
PIC
PIC999V99
999V99 12345
12345
Receiving
Receiving
Picture
Result
Picture
Result
123.45
PIC
PIC999.99
999.99
PIC
PIC999V99
999V99 02345
02345
PIC
PIC999.9
999.9
PIC
PIC999V99
999V99 51234
51234
PIC
PIC99.99
99.99
PIC
PIC999
999
PIC
PIC999.99
999.99
456
456
023.4
12.34
456.00
Receiving
Receiving
Picture
Result
Picture
Result
-123
PIC
PIC-999
-999
123PIC
PIC 999999PIC
123
PIC-999
-999
+12345
+12345 PIC
PIC+9(5)
+9(5)
-123
PIC
-123
PIC+9(3)
+9(3)
-123
PIC
-123
PIC999+
999+
+12345
-123
123-
1234
1234
0000
0000
Receiving
Receiving
Picture
Result
Picture
Result
1234
PIC
PIC9(4)CR
9(4)CR
1234CR
PIC
PIC9(4)CR
9(4)CR
1223
PIC
9(4)DB
PIC 9(4)DB
1234DB
PIC
9(4)DB
PIC 9(4)DB
PIC
PIC$99999
$99999
PIC
PIC$ZZZZZ
$ZZZZZ
$01234
$
Floating Insertion.
Sending
Sending
Picture
Data
Picture
Data
PIC
0000
PIC9(4)
9(4)
0000
PIC
0080
PIC9(4)
9(4)
0080
PIC
0128
PIC9(4)
9(4)
0128
PIC
57397
PIC9(5)
9(5)
57397
PIC
PICS9(4)
S9(4)
PIC
PICS9(4)
S9(4)
PIC
PICS9(4)
S9(4)
PIC
PICS9(5)
S9(5)
--0005
0005
+0080
+0080
--0080
0080
+71234
+71234
Receiving
Receiving
Picture
Result
Picture
Result
$0.00
PIC
PIC$$,$$9.99
$$,$$9.99
$80.00
PIC
$$,$$9.00
PIC $$,$$9.00
$128.00
PIC
PIC$$,$$9.99
$$,$$9.99
$7,397
PIC
PIC$$,$$9
$$,$$9
PIC
PIC++++9
++++9
PIC
PIC++++9
++++9
PIC
PIC-- ------ 99
PIC
PIC-- ------ 99
-5
+80
-80
1234
Sending
Sending
Picture
Data
Picture
Data
PIC
12345
PIC9(5)
9(5)
12345
PIC
01234
PIC9(5)
9(5)
01234
PIC
00123
PIC9(5)
9(5)
00123
PIC
00012
PIC9(5)
9(5)
00012
PIC
05678
PIC9(5)
9(5)
05678
PIC
00567
PIC9(5)
9(5)
00567
PIC
00000
PIC9(5)
9(5)
00000
Receiving
Receiving
Picture
Result
Picture
Result
PIC
12,345
PICZZ,999
ZZ,999
1,234
PIC
PICZZ,999
ZZ,999
123
PIC
ZZ,999
PIC ZZ,999
012
PIC
ZZ,999
PIC ZZ,999
*5,678
PIC
PIC**,**9
**,**9
***567
PIC
******
PIC**,**9
**,**9
PIC
PIC**,***
**,***
The
USAGE
clause
USAGE IS DISPLAY
SYSTEM CHAR HEX DEC 8 4 2 1 8 4 2 1
ASCII
"A"
41
65
0 1 0 0 0 0 0 1
EBCDIC
"A"
C1
193
1 1 0 0 0 0 0 1
0 0 1 1 0 1 0 0 41
"4"
0 0 1 1 0 0 0 1 31
"1"
Num3 PIC 9.
0 0 0 0 0 0 0 0 0
nul
0 0 1 1 0 1 0 0 41
"4"
0 0 1 1 0 0 0 1 31
"1"
Num3 PIC 9.
0 1 1 0 0 1 0 1 65
"e"
USAGE Syntax
01 Num1
01 Num2
USAGE IS COMP
Number of Digits
Storage Required.
1 TO 4
5 TO 9
10 TO 18
USAGE IS COMP-1
Number of Digits
Storage Required.
USAGE IS COMP-1.
01 WS-COMP-1 COMP-1.
USAGE IS COMP-2
Number of Digits
Storage Required.
USAGE IS COMP- 2.
01 WS-COMP- 2 COMP- 2.
USAGE IS COMP-3
Number of Digits
Storage Required.
32
16
USAGE IS PACKED-DECIMAL
PIC S9
VALUE +5
PIC S9(2)
VALUE -32
PIC S9(3)
VALUE +262
D O G
Word1
Number
Word2
Word3
= 2 Byte boundary
= 4 Byte boundary
= 8 Byte boundary
= 4 Byte boundary
D O G
Word1
Word2
Number
Word3
01 FiveBytes
01 FourBytes
F R O G S
Word1
= 2 Byte boundary
= 4 Byte boundary
= 8 Byte boundary
= 4 Byte boundary
NumberNumber
Word2
Word3
01 FiveBytes
01 FourBytes
44byte
byteboundary
boundary
F R O G S
Word1
= 2 Byte boundary
= 4 Byte boundary
= 8 Byte boundary
= 4 Byte boundary
NumberNumber
Word2
Word3
Conditions
IF Syntax.
StatementBlock
IF Condition THEN
NEXT
SENTENCE
StatementBlock
END - IF
ELSE
NEXT
SENTENCE
CONDITION TYPES
Simple
SimpleConditions
Conditions
Relation
Relation
Conditions
Conditions
Class Conditions
Class Conditions
Sign Conditions
Sign Conditions
Complex
ComplexConditions
Conditions
Condition
ConditionNames
Names
Relation Conditions
NOT GREATER THAN
NOT >
NOT
NOT
NOT
NOT
Identifier
Literal
IS
ArithmeticExpression
LESS THAN
<
EQUAL TO
Identifier
Literal
ArithmeticExpression
LESS
THAN
OR
EQUAL
TO
<=
Class Conditions.
NUMERIC
ALPHABETIC
UserDefinedClassName
Sign Conditions
POSITIVE
Complex conditions.
AND
Condition
Condition
OR
NOT
NOT
AND
AND
==
==
****
**or
or//
3.
3.
OR
OR
== ++or
or--
Examples
) THEN
IF ( Row > 0) AND( Row < 26
DISPLAY On Screen
END-IF
) OR( VarA NOT = VarF
)
IF ( VarA > VarC ) OR( VarC = VarD
DISPLAY Done
END-IF
Implied Subjects.
Nested IFs
IF
IF (( VarA
VarA << 10
10 )) AND
AND (( VarB
VarB NOT
NOT >> VarC
VarC )) THEN
THEN
IF
IF VarG
VarG == 14
14 THEN
THEN
DISPLAY
DISPLAY First
First
ELSE
ELSE
DISPLAY
DISPLAY Second
Second
END-IF
END-IF
ELSE
ELSE
DISPLAY
DISPLAY Third
Third
END-IF
END-IF
VarA
VarA VarB
VarB VarC
VarC VarG
VarG
33 T
33 T
33 T
F
13
13
44 T
44 T
44 F
44 T
15
15
15
15
33
15
15
14
14 T
F
15
15
14
14
14
14
DISPLAY
DISPLAY
First
Second
Third
Third
Condition Names.
IF Var A GREATER THAN Var B THEN Action
Condition is either
TRUE or FALSE
Example.
IF StudentRecord = HIGH-VALUES THEN Action
The statement above may be replaced by the one below. The
condition name EndOfStudentFile may be used instead of the
condition StudentRecord = HIGH-VALUES.
IF EndOfStudentFile THEN Action
VALUE
THROUGH
88 ConditionName
HighValue
VALUES LowValue
THRU
They are always associated with a data item and are defined
immediately after the definition of the data item.
The VALUE clause is used to identify the values which make the
Condition Name TRUE.
01
01 CityCode
CityCode
88
88 Dublin
Dublin
88
88 Limerick
Limerick
88
88 Cork
Cork
88
88 Galway
Galway
88
88 Sligo
Sligo
88
88 Waterford
Waterford
88
88 UniversityCity
UniversityCity
IF
IF Limerick
Limerick
DISPLAY
DISPLAY "Hey,
"Hey, we're
we're home."
home."
END-IF
END-IF
IF
IF UniversityCity
UniversityCity
PERFORM
PERFORM CalcRentSurcharge
CalcRentSurcharge
END-IF
END-IF
PIC
PIC 99
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE 5.
5.
1.
1.
2.
2.
3.
3.
4.
4.
5.
5.
6.
6.
11 THRU
THRU 4.
4.
City Code
5
Dublin
Limerick
Cork
Galway
Sligo
Waterford
UniversityCity
FALSE
FALSE
FALSE
FALSE
TRUE
FALSE
FALSE
01
01 CityCode
CityCode
88
88 Dublin
Dublin
88
88 Limerick
Limerick
88
88 Cork
Cork
88
88 Galway
Galway
88
88 Sligo
Sligo
88
88 Waterford
Waterford
88
88 UniversityCity
UniversityCity
IF
IF Limerick
Limerick
DISPLAY
DISPLAY "Hey,
"Hey, we're
we're home."
home."
END-IF
END-IF
IF
IF UniversityCity
UniversityCity
PERFORM
PERFORM CalcRentSurcharge
CalcRentSurcharge
END-IF
END-IF
PIC
PIC 99
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE 5.
5.
1.
1.
2.
2.
3.
3.
4.
4.
5.
5.
6.
6.
11 THRU
THRU 4.
4.
City Code
2
Dublin
Limerick
Cork
Galway
Sligo
Waterford
UniversityCity
FALSE
TRUE
FALSE
FALSE
FALSE
FALSE
TRUE
01
01 CityCode
CityCode
88
88 Dublin
Dublin
88
88 Limerick
Limerick
88
88 Cork
Cork
88
88 Galway
Galway
88
88 Sligo
Sligo
88
88 Waterford
Waterford
88
88 UniversityCity
UniversityCity
IF
IF Limerick
Limerick
DISPLAY
DISPLAY "Hey,
"Hey, we're
we're home."
home."
END-IF
END-IF
IF
IF UniversityCity
UniversityCity
PERFORM
PERFORM CalcRentSurcharge
CalcRentSurcharge
END-IF
END-IF
PIC
PIC 99
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE 5.
5.
1.
1.
2.
2.
3.
3.
4.
4.
5.
5.
6.
6.
11 THRU
THRU 4.
4.
City Code
6
Dublin
Limerick
Cork
Galway
Sligo
Waterford
UniversityCity
FALSE
FALSE
FALSE
FALSE
FALSE
TRUE
FALSE
01
PIC
01 InputChar
InputChar
PIC X.
X.
88
VALUE
88 Vowel
Vowel
VALUE "A","E","I","O","U".
"A","E","I","O","U".
88
88 Consonant
Consonant VALUE
VALUE "B"
"B" THRU
THRU "D",
"D", "F","G","H"
"F","G","H"
"J"
"J" THRU
THRU "N",
"N", "P"
"P" THRU
THRU "T"
"T"
"V"
"V" THRU
THRU "Z".
"Z".
88
VALUE
88 Digit
Digit
VALUE "0"
"0" THRU
THRU "9".
"9".
88
88 LowerCase
LowerCase VALUE
VALUE "a"
"a" THRU
THRU "z".
"z".
88
88 ValidChar
ValidChar VALUE
VALUE "A"
"A" THRU
THRU "Z","0"
"Z","0" THRU
THRU "9".
"9".
IF
IF ValidChar
ValidChar
DISPLAY
DISPLAY "Input
"Input OK."
OK."
END-IF
END-IF
IF
IF LowerCase
LowerCase
DISPLAY
DISPLAY "Not
"Not Upper
Upper Case"
Case"
END-IF
END-IF
IF
IF Vowel
Vowel
Display
Display "Vowel
"Vowel entered."
entered."
END-IF
END-IF
Input Char
E
Vowel
Consonant
Digit
LowerCase
ValidChar
TRUE
FALSE
FALSE
FALSE
TRUE
01
PIC
01 InputChar
InputChar
PIC X.
X.
88
VALUE
88 Vowel
Vowel
VALUE "A","E","I","O","U".
"A","E","I","O","U".
88
88 Consonant
Consonant VALUE
VALUE "B"
"B" THRU
THRU "D",
"D", "F","G","H"
"F","G","H"
"J"
"J" THRU
THRU "N",
"N", "P"
"P" THRU
THRU "T"
"T"
"V"
"V" THRU
THRU "Z".
"Z".
88
VALUE
88 Digit
Digit
VALUE "0"
"0" THRU
THRU "9".
"9".
88
88 LowerCase
LowerCase VALUE
VALUE "a"
"a" THRU
THRU "z".
"z".
88
88 ValidChar
ValidChar VALUE
VALUE "A"
"A" THRU
THRU "Z","0"
"Z","0" THRU
THRU "9".
"9".
IF
IF ValidChar
ValidChar
DISPLAY
DISPLAY "Input
"Input OK."
OK."
END-IF
END-IF
IF
IF LowerCase
LowerCase
DISPLAY
DISPLAY "Not
"Not Upper
Upper Case"
Case"
END-IF
END-IF
IF
IF Vowel
Vowel
Display
Display "Vowel
"Vowel entered."
entered."
END-IF
END-IF
Input Char
4
Vowel
Consonant
Digit
LowerCase
ValidChar
FALSE
FALSE
TRUE
FALSE
TRUE
01
PIC
01 InputChar
InputChar
PIC X.
X.
88
VALUE
88 Vowel
Vowel
VALUE "A","E","I","O","U".
"A","E","I","O","U".
88
88 Consonant
Consonant VALUE
VALUE "B"
"B" THRU
THRU "D",
"D", "F","G","H"
"F","G","H"
"J"
"J" THRU
THRU "N",
"N", "P"
"P" THRU
THRU "T"
"T"
"V"
"V" THRU
THRU "Z".
"Z".
88
VALUE
88 Digit
Digit
VALUE "0"
"0" THRU
THRU "9".
"9".
88
88 LowerCase
LowerCase VALUE
VALUE "a"
"a" THRU
THRU "z".
"z".
88
88 ValidChar
ValidChar VALUE
VALUE "A"
"A" THRU
THRU "Z","0"
"Z","0" THRU
THRU "9".
"9".
IF
IF ValidChar
ValidChar
DISPLAY
DISPLAY "Input
"Input OK."
OK."
END-IF
END-IF
IF
IF LowerCase
LowerCase
DISPLAY
DISPLAY "Not
"Not Upper
Upper Case"
Case"
END-IF
END-IF
IF
IF Vowel
Vowel
Display
Display "Vowel
"Vowel entered."
entered."
END-IF
END-IF
Input Char
g
Vowel
Consonant
Digit
LowerCase
ValidChar
FALSE
FALSE
FALSE
TRUE
FALSE
EndOfFileFlag
01
01 EndOfFileFlag
EndOfFileFlag
88
88 EndOfFile
EndOfFile
PIC
PIC 99 VALUE
VALUE 0.
0.
VALUE
VALUE 1.
1.
0
EndOfFile
READ
READ InFile
InFile
AT
AT END
END MOVE
MOVE 11 TO
TO EndOfFileFlag
EndOfFileFlag
END-READ
END-READ
PERFORM
PERFORM UNTIL
UNTIL EndOfFile
EndOfFile
Statements
Statements
READ
READ InFile
InFile
AT
AT END
END MOVE
MOVE 11 TO
TO EndOfFileFlag
EndOfFileFlag
END-READ
END-READ
END-PERFORM
END-PERFORM
EndOfFileFlag
01
01 EndOfFileFlag
EndOfFileFlag
88
88 EndOfFile
EndOfFile
PIC
PIC 99 VALUE
VALUE 0.
0.
VALUE
VALUE 1.
1.
1
EndOfFile
READ
READ InFile
InFile
AT
AT END
END MOVE
MOVE 11 TO
TO EndOfFileFlag
EndOfFileFlag
END-READ
END-READ
PERFORM
PERFORM UNTIL
UNTIL EndOfFile
EndOfFile
Statements
Statements
READ
READ InFile
InFile
AT
AT END
END MOVE
MOVE 11 TO
TO EndOfFileFlag
EndOfFileFlag
END-READ
END-READ
END-PERFORM
END-PERFORM
0
EndOfFile
1
NotEndOfFile 0
READ
READ InFile
InFile
AT
AT END
END SET
SET EndOfFile
EndOfFile TO
TO TRUE
TRUE
END-READ
END-READ
PERFORM
PERFORM UNTIL
UNTIL EndOfFile
EndOfFile
Statements
Statements
READ
READ InFile
InFile
AT
AT END
END SET
SET EndOfFile
EndOfFile TO
TO TRUE
TRUE
END-READ
END-READ
END-PERFORM
END-PERFORM
Set
Set NotEndOfFile
NotEndOfFile TO
TO TRUE.
TRUE.
1
EndOfFile
1
NotEndOfFile 0
READ
READ InFile
InFile
AT
AT END
END SET
SET EndOfFile
EndOfFile TO
TO TRUE
TRUE
END-READ
END-READ
PERFORM
PERFORM UNTIL
UNTIL EndOfFile
EndOfFile
Statements
Statements
READ
READ InFile
InFile
AT
AT END
END SET
SET EndOfFile
EndOfFile TO
TO TRUE
TRUE
END-READ
END-READ
END-PERFORM
END-PERFORM
Set
Set NotEndOfFile
NotEndOfFile TO
TO TRUE.
TRUE.
0
EndOfFile
1
NotEndOfFile 0
READ
READ InFile
InFile
AT
AT END
END SET
SET EndOfFile
EndOfFile TO
TO TRUE
TRUE
END-READ
END-READ
PERFORM
PERFORM UNTIL
UNTIL EndOfFile
EndOfFile
Statements
Statements
READ
READ InFile
InFile
AT
AT END
END SET
SET EndOfFile
EndOfFile TO
TO TRUE
TRUE
END-READ
END-READ
END-PERFORM
END-PERFORM
Set
Set NotEndOfFile
NotEndOfFile TO
TO TRUE.
TRUE.
FD
FD StudentFile.
StudentFile.
01
StudentDetails.
01 StudentDetails.
88
88 EndOfFile
EndOfFile VALUE
VALUE HIGH-VALUES.
HIGH-VALUES.
02
StudentId
PIC
02 StudentId
PIC 9(7).
9(7).
02
StudentName.
02 StudentName.
03
PIC
03 Surname
Surname
PIC X(8).
X(8).
03
Initials
PIC
XX.
03 Initials
PIC XX.
02
DateOfBirth.
02 DateOfBirth.
03
PIC
03 YOBirth
YOBirth
PIC 9(2).
9(2).
03
MOBirth
PIC
9(2).
03 MOBirth
PIC 9(2).
03
DOBirth
PIC
03 DOBirth
PIC 9(2).
9(2).
02
CourseCode
PIC
X(4).
02 CourseCode
PIC X(4).
02
Grant
PIC
02 Grant
PIC 9(4).
9(4).
02
Gender
PIC
X.
02 Gender
PIC X.
PROCEDURE
PROCEDURE DIVISION.
DIVISION.
Begin.
Begin.
OPEN
OPEN INPUT
INPUT StudentFile
StudentFile
READ
StudentFile
READ StudentFile
AT
AT END
END SET
SET EndOfFile
EndOfFile TO
TO TRUE
TRUE
END-READ
END-READ
PERFORM
PERFORM UNTIL
UNTIL EndOfFile
EndOfFile
DISPLAY
StudentId
DISPLAY StudentId SPACE
SPACE StudentName
StudentName SPACE
SPACE CourseCode
CourseCode
READ
StudentFile
READ StudentFile
AT
AT END
END SET
SET EndOfFile
EndOfFile TO
TO TRUE
TRUE
END-READ
END-READ
END-PERFORM
END-PERFORM
CLOSE
CLOSE StudentFile
StudentFile
STOP
RUN.
STOP RUN.
The Evaluate
Identifier
Literal
CondExpression
EVALUATE
ArithExpression
TRUE
FALSE
ANY
Condition
WHEN TRUE
FALSE
Identifier
NOT
Literal
ArithExpression
Identifier
THRU
Literal
THROUGH
ArithExpression
StatementBlock
10
EVALUATE TRUE
Position
WHEN L-Arrow
2 THRU 10 PERFORM MoveLeft
WHEN R-Arrow
1 THRU 9 PERFORM MoveRight
WHEN L-Arrow
1
MOVE 10 TO Position
WHEN R-Arrow
10
MOVE 1 TO Position
WHEN DeleteKey
1
PERFORM CantDelete
WHEN Character
ANY
PERFORM InsertChar
WHEN OTHER PERFORM DisplayErrorMessage
END-EVALUATE
TRUE
Age<20
Age<20
Age>19 AND <41
Age>19 AND <41
Age>40
Age>40
:
:
ANY
M
40>
<10
20
F
40>
<10
15
TRUE
ANY
ANY
Service<10
Service<10
Service<10
Service<10
:
:
Service>20
M
F
20-40 20-40 etc
10-20 10-20 etc
14
23
MOVE 5 TO Bonus
MOVE 10 TO Bonus
MOVE 12 TO Bonus
MOVE 13 TO Bonus
MOVE 20 TO Bonus
MOVE 15 TO Bonus
:
:
MOVE 25 TO Bonus
COPY Verb
The COPY verb is generally used when creating large software systems.
Each client program which wants to use items described in the copy library
uses the COPY verb to include the descriptions it requires.
When more than one copy library is used the OF or IN qualifier is used to
indicate which library is being referenced.
COPY format
TextName
OF
COPY
ExternalFileNameLiteral
IN
LibraryName
LibraryNameLiteral
== PseudoText1 ==
== PseudoText2 ==
Identifier2
Identifier1
REPLACING
BY
Literal1
Literal2
Word1
Word2
LibraryNameLiteral
LibraryNameLiteral
If the COPY does use the REPLACING phrase then the text is
copied and each properly matched occurrence of PseudoText-1, Identifier-1, Literal-1 and Word-1 in the library text is
replaced by the corresponding Pseudo-Text-2, Identifier-2,
Literal-2 or Word-2 in the REPLACING phrase.
Text-Words
A text word is ;
Text-Word Examples
MOVE
1 Text Word
PIC S9(4)V9(6)
PIC S9(4)V9(6)
4 ) V9 ( 6 )
COPY Example 1
IDENTIFICATION
IDENTIFICATION DIVISION.
DIVISION.
PROGRAM-ID.
COPYEG1.
PROGRAM-ID. COPYEG1.
AUTHOR.
AUTHOR. Michael
Michael Coughlan.
Coughlan.
ENVIRONMENT
ENVIRONMENT DIVISION.
DIVISION.
01
FILE-CONTROL.
01 StudentRec.
StudentRec.
FILE-CONTROL.
SELECT
SELECT StudentFile
StudentFile ASSIGN
ASSIGN TO
TO "STUDENTS.DAT"
"STUDENTS.DAT"
88
ORGANIZATION
IS
LINE
SEQUENTIAL.
88 EndOfSF
EndOfSF
ORGANIZATION IS LINE SEQUENTIAL.
DATA
DATA DIVISION.
DIVISION.
FILE
SECTION.
FILE SECTION.
FD
FD StudentFile.
StudentFile.
COPY
COPY COPYFILE1.
COPYFILE1.
VALUE
VALUE HIGH-VALUES.
HIGH-VALUES.
02
02 StudentNumber
StudentNumber
PIC
PIC 9(7).
9(7).
02
02 StudentName
StudentName
PIC
PIC X(60).
X(60).
02
02 CourseCode
CourseCode
PIC
PIC X(4).
X(4).
02
PIC
PROCEDURE
02 FeesOwed
FeesOwed
PIC 9(4).
9(4).
PROCEDURE DIVISION.
DIVISION.
BeginProg.
BeginProg.
OPEN
02
PIC
OPEN INPUT
INPUT StudentFile
StudentFile
02 AmountPaid
AmountPaid
PIC 9(4)V99.
9(4)V99.
READ
StudentFile
READ StudentFile
AT
AT END
END SET
SET EndOfSF
EndOfSF TO
TO TRUE
TRUE
END-READ
END-READ
PERFORM
PERFORM UNTIL
UNTIL EndOfSF
EndOfSF
DISPLAY
StudentNumber
DISPLAY StudentNumber SPACE
SPACE StudentName
StudentName SPACE
SPACE
CourseCode
SPACE
FeesOwed
SPACE
AmountPaid
CourseCode SPACE FeesOwed SPACE AmountPaid
READ
StudentFile
READ StudentFile
AT
AT END
END SET
SET EndOfSF
EndOfSF TO
TO TRUE
TRUE
END-READ
END-READ
END-PERFORM
END-PERFORM
GOBACK.
GOBACK.
COPY Example 2
Copyfile2.cbl
02
TIMES
02 StudName
StudName PIC
PIC X(20)
X(20) OCCURS
OCCURS XYZ
XYZ TIMES.
TIMES.
TIMES
01 NameTable2.
COPY "CopyFile2.CBL" REPLACING XYZ BY 120.
01 NameTable2.
02 StudName PIC X(20) OCCURS 120 TIMES.
TIMES
COPY Example 3
Copyfile3.cbl
02
02 CustOrder
CustOrder
PIC
PIC 9(R).
9(R).
01 CopyData.
COPY Copyfile3 REPLACING ==R== BY ==4==.
01 CopyData.
02 CustOrder
PIC 9(4).
COPY Example 4
Copyfile4.cbl
02
02 CustOrder2
CustOrder2
PIC
PIC 9(6)V99.
9(6)V99.
01 CopyData.
COPY CopyFile4 REPLACING ==V99== BY ====.
01 CopyData.
02 CustOrder2
PIC 9(6).
COPY Example 5
Copyfile5.cbl
02
02 CustKey
CustKey
PIC
PIC X(3)
X(3) VALUE
VALUE "KEY".
"KEY".
01 CopyData.
COPY CopyFile5 REPLACING "KEY" BY "ABC".
01 CopyData.
02 CustKey
COPY Example 6
Copyfile5.cbl
02
02 CustKey
CustKey
PIC
PIC X(3)
X(3) VALUE
VALUE "KEY".
"KEY".
01 CopyData.
COPY CopyFile5 REPLACING "CustKey" BY "Cust".
01 CopyData.
02 CustKey
COPY Example 7
Copyfile5.cbl
02
02 CustKey
CustKey
PIC
PIC X(3)
X(3) VALUE
VALUE "KEY".
"KEY".
01 CopyData.
COPY CopyFile5 REPLACING "KEY" BY =="ABC".
02 CustNum
PIC 9(8)==.
01 CopyData.
02 CustKey
02 CustNum
COPY Example 8
Copyfile5.cbl
02
02 CustKey
CustKey
PIC
PIC X(3)
X(3) VALUE
VALUE "KEY".
"KEY".
02
02
02
CustKey
CustKey
CustKey
PIC
PIC
PIC
Copyfile5.cbl
COPY Example 9
02
02 CustKey
CustKey PIC
PIC X(3)
X(3) VALUE
VALUE "KEY".
"KEY".
* the X before (3) is a textword replaced by pseudotext "Replace the X"
02
02
02
CustKey
CustKey
CustKey
PIC
PIC
PIC
Replace the
X(19) VALUE
X(19) VALUE
COPY Example 10
Copyfile5.cbl
02
02 CustKey
CustKey PIC
PIC X(3)
X(3) VALUE
VALUE "KEY".
"KEY".
The
PERFORM
Paragraphs :- Revisited
Paragraph Example
ProcessRecord.
DISPLAY StudentRecord
READ StudentFile
AT END MOVE HIGH-VALUES TO StudentRecord
END-READ.
ProduceOutput.
DISPLAY Here is a message.
NOTE
NOTE
The
Thescope
scopeof
ofProcessRecord
ProcessRecordis
is
delimited
delimitedby
bythe
theoccurrence
occurrencethe
the
paragraph
paragraphname
nameProduceOutput.
ProduceOutput.
Format 1 Syntax.
THRU
EndProc
PERFORM 1stProc
THROUGH
Format 1 Example.
Run of PerformFormat1
In
In TopLevel.
TopLevel. Starting
Starting to
to run
run program
program
>>>>
>>>> Now
Now in
in OneLevelDown
OneLevelDown
>>>>>>>>
Now
>>>>>>>> Now in
in TwoLevelsDown.
TwoLevelsDown.
>>>>
Back
in
OneLevelDown
>>>> Back in OneLevelDown
Back
Back in
in TopLevel.
TopLevel.
PROCEDURE
PROCEDURE DIVISION.
DIVISION.
TopLevel.
TopLevel.
DISPLAY
DISPLAY"In
"In TopLevel.
TopLevel.Starting
Startingto
torun
runprogram"
program"
PERFORM
PERFORM OneLevelDown
OneLevelDown
DISPLAY
"Back
DISPLAY "Back in
in TopLevel.".
TopLevel.".
STOP
RUN.
STOP RUN.
TwoLevelsDown.
TwoLevelsDown.
DISPLAY
DISPLAY ">>>>>>>>
">>>>>>>> Now
Now in
in TwoLevelsDown."
TwoLevelsDown."
OneLevelDown.
OneLevelDown.
DISPLAY
DISPLAY ">>>>
">>>> Now
Now in
in OneLevelDown"
OneLevelDown"
PERFORM
TwoLevelsDown
PERFORM TwoLevelsDown
DISPLAY
DISPLAY ">>>>
">>>> Back
Back in
in OneLevelDown".
OneLevelDown".
Format 1 Example.
Run of PerformFormat1
In
In TopLevel.
TopLevel. Starting
Starting to
to run
run program
program
>>>>
Now
in
OneLevelDown
>>>> Now in OneLevelDown
>>>>>>>>
>>>>>>>> Now
Now in
in TwoLevelsDown.
TwoLevelsDown.
>>>>
Back
in
OneLevelDown
>>>> Back in OneLevelDown
Back
Back in
in TopLevel.
TopLevel.
PROCEDURE
PROCEDURE DIVISION.
DIVISION.
TopLevel.
TopLevel.
DISPLAY
DISPLAY "In
"In TopLevel.
TopLevel. Starting
Starting to
to run
run program"
program"
PERFORM
PERFORMOneLevelDown
OneLevelDown
DISPLAY
DISPLAY "Back
"Back in
in TopLevel.".
TopLevel.".
STOP
RUN.
STOP RUN.
TwoLevelsDown.
TwoLevelsDown.
DISPLAY
DISPLAY ">>>>>>>>
">>>>>>>> Now
Now in
in TwoLevelsDown."
TwoLevelsDown."
OneLevelDown.
OneLevelDown.
DISPLAY
DISPLAY ">>>>
">>>> Now
Now in
in OneLevelDown"
OneLevelDown"
PERFORM
TwoLevelsDown
PERFORM TwoLevelsDown
DISPLAY
DISPLAY ">>>>
">>>> Back
Back in
in OneLevelDown".
OneLevelDown".
Format 1 Example.
Run of PerformFormat1
In
In TopLevel.
TopLevel. Starting
Starting to
to run
run program
program
>>>>
>>>> Now
Now in
in OneLevelDown
OneLevelDown
>>>>>>>>
>>>>>>>> Now
Now in
in TwoLevelsDown.
TwoLevelsDown.
>>>>
Back
in
OneLevelDown
>>>> Back in OneLevelDown
Back
Back in
in TopLevel.
TopLevel.
PROCEDURE
PROCEDURE DIVISION.
DIVISION.
TopLevel.
TopLevel.
DISPLAY
DISPLAY "In
"In TopLevel.
TopLevel. Starting
Starting to
to run
run program"
program"
PERFORM
OneLevelDown
PERFORM OneLevelDown
DISPLAY
DISPLAY "Back
"Back in
in TopLevel.".
TopLevel.".
GOBACK.
GOBACK.
TwoLevelsDown.
TwoLevelsDown.
DISPLAY
DISPLAY ">>>>>>>>
">>>>>>>> Now
Now in
in TwoLevelsDown."
TwoLevelsDown."
OneLevelDown.
OneLevelDown.
DISPLAY
DISPLAY">>>>
">>>> Now
Now in
in OneLevelDown"
OneLevelDown"
PERFORM
PERFORM
DISPLAY
DISPLAY
TwoLevelsDown
TwoLevelsDown
">>>>
">>>> Back
Back in
in OneLevelDown".
OneLevelDown".
Format 1 Example.
Run of PerformFormat1
In
In TopLevel.
TopLevel. Starting
Starting to
to run
run program
program
>>>>
Now
in
OneLevelDown
>>>> Now in OneLevelDown
>>>>>>>>
>>>>>>>> Now
Now in
in TwoLevelsDown.
TwoLevelsDown.
>>>>
Back
in
OneLevelDown
>>>> Back in OneLevelDown
Back
Back in
in TopLevel.
TopLevel.
PROCEDURE
PROCEDURE DIVISION.
DIVISION.
TopLevel.
TopLevel.
DISPLAY
DISPLAY "In
"In TopLevel.
TopLevel. Starting
Starting to
to run
run program"
program"
PERFORM
OneLevelDown
PERFORM OneLevelDown
DISPLAY
DISPLAY "Back
"Back in
in TopLevel.".
TopLevel.".
GOBACK.
GOBACK.
TwoLevelsDown.
TwoLevelsDown.
DISPLAY
DISPLAY ">>>>>>>>
">>>>>>>> Now
Now in
in TwoLevelsDown."
TwoLevelsDown."
OneLevelDown.
OneLevelDown.
DISPLAY
DISPLAY ">>>>
">>>> Now
Now in
in OneLevelDown"
OneLevelDown"
PERFORM
PERFORMTwoLevelsDown
TwoLevelsDown
DISPLAY
DISPLAY ">>>>
">>>> Back
Back in
in OneLevelDown".
OneLevelDown".
Format 1 Example.
Run of PerformFormat1
In
In TopLevel.
TopLevel. Starting
Starting to
to run
run program
program
>>>>
Now
in
OneLevelDown
>>>> Now in OneLevelDown
>>>>>>>>
>>>>>>>> Now
Now in
in TwoLevelsDown.
TwoLevelsDown.
>>>>
>>>> Back
Back in
in OneLevelDown
OneLevelDown
Back
in
TopLevel.
Back in TopLevel.
PROCEDURE
PROCEDURE DIVISION.
DIVISION.
TopLevel.
TopLevel.
DISPLAY
DISPLAY "In
"In TopLevel.
TopLevel. Starting
Starting to
to run
run program"
program"
PERFORM
OneLevelDown
PERFORM OneLevelDown
DISPLAY
DISPLAY "Back
"Back in
in TopLevel.".
TopLevel.".
GOBACK.
GOBACK.
TwoLevelsDown.
TwoLevelsDown.
DISPLAY
DISPLAY">>>>>>>>
">>>>>>>> Now
Now in
inTwoLevelsDown."
TwoLevelsDown."
OneLevelDown.
OneLevelDown.
DISPLAY
DISPLAY ">>>>
">>>> Now
Now in
in OneLevelDown"
OneLevelDown"
PERFORM
TwoLevelsDown
PERFORM TwoLevelsDown
DISPLAY
DISPLAY ">>>>
">>>> Back
Back in
in OneLevelDown".
OneLevelDown".
Format 1 Example.
Run of PerformFormat1
In
In TopLevel.
TopLevel. Starting
Starting to
to run
run program
program
>>>>
Now
in
OneLevelDown
>>>> Now in OneLevelDown
>>>>>>>>
>>>>>>>> Now
Now in
in TwoLevelsDown.
TwoLevelsDown.
>>>>
>>>> Back
Back in
in OneLevelDown
OneLevelDown
Back
Back in
in TopLevel.
TopLevel.
PROCEDURE
PROCEDURE DIVISION.
DIVISION.
TopLevel.
TopLevel.
DISPLAY
DISPLAY "In
"In TopLevel.
TopLevel. Starting
Starting to
to run
run program"
program"
PERFORM
OneLevelDown
PERFORM OneLevelDown
DISPLAY
DISPLAY "Back
"Back in
in TopLevel.".
TopLevel.".
GOBACK.
GOBACK.
TwoLevelsDown.
TwoLevelsDown.
DISPLAY
DISPLAY ">>>>>>>>
">>>>>>>> Now
Now in
in TwoLevelsDown."
TwoLevelsDown."
OneLevelDown.
OneLevelDown.
DISPLAY
DISPLAY
PERFORM
PERFORM
">>>>
">>>> Now
Now in
in OneLevelDown"
OneLevelDown"
TwoLevelsDown
TwoLevelsDown
DISPLAY
DISPLAY">>>>
">>>> Back
Back in
inOneLevelDown".
OneLevelDown".
Format 1 Example.
Run of PerformFormat1
In
In TopLevel.
TopLevel. Starting
Starting to
to run
run program
program
>>>>
Now
in
OneLevelDown
>>>> Now in OneLevelDown
>>>>>>>>
>>>>>>>> Now
Now in
in TwoLevelsDown.
TwoLevelsDown.
>>>>
Back
in
OneLevelDown
>>>> Back in OneLevelDown
Back
Back in
in TopLevel.
TopLevel.
PROCEDURE
PROCEDURE DIVISION.
DIVISION.
TopLevel.
TopLevel.
DISPLAY
DISPLAY
PERFORM
PERFORM
"In
"In TopLevel.
TopLevel. Starting
Starting to
to run
run program"
program"
OneLevelDown
OneLevelDown
DISPLAY
DISPLAY"Back
"Backin
in TopLevel.".
TopLevel.".
GOBACK.
GOBACK.
TwoLevelsDown.
TwoLevelsDown.
DISPLAY
DISPLAY ">>>>>>>>
">>>>>>>> Now
Now in
in TwoLevelsDown."
TwoLevelsDown."
OneLevelDown.
OneLevelDown.
DISPLAY
DISPLAY ">>>>
">>>> Now
Now in
in OneLevelDown"
OneLevelDown"
PERFORM
TwoLevelsDown
PERFORM TwoLevelsDown
DISPLAY
DISPLAY ">>>>
">>>> Back
Back in
in OneLevelDown".
OneLevelDown".
Statements
Statements
Statements
Statements
IF
IF NoErrorFound
NoErrorFound
Statements
Statements
Statements
Statements
IF
IF NoErrorFound
NoErrorFound
Statements
Statements
Statements
Statements
Statements
Statements
END-IF
END-IF
END-IF.
END-IF.
Statements
Statements
Statements
Statements
IF
IF ErrorFound
ErrorFound GO
GO TO
TO SumSalesExit
SumSalesExit
END-IF
END-IF
Statements
Statements
Statements
Statements
Statements
Statements
IF
IF ErrorFound
ErrorFound GO
GO TO
TO SumSalesExit
SumSalesExit
END-IF
END-IF
Statements
Statements
SumSalesExit.
SumSalesExit.
EXIT.
EXIT.
Format 2 - Syntax
THRU
EndProc
PERFORM 1stProc
THROUGH
RepeatCount TIMES
GOBACK.
DisplayName.
DisplayName.
DISPLAY Tom Ryan.
Format 2 Example
$$ SET
SET SOURCEFORMAT"FREE"
SOURCEFORMAT"FREE"
IDENTIFICATION
IDENTIFICATION DIVISION.
DIVISION.
PROGRAM-ID.
PerformExample2.
PROGRAM-ID. PerformExample2.
AUTHOR.
AUTHOR. CGI.
CGI.
DATA
DATA DIVISION.
DIVISION.
WORKING-STORAGE
WORKING-STORAGE SECTION.
SECTION.
01
NumofTimes
PIC
01 NumofTimes
PIC 99 VALUE
VALUE 5.
5.
Run of PerformExample2
Starting to run program
Starting to run program
>>>>This is an in line Perform
>>>>This is an in line Perform
>>>>This is an in line Perform
>>>>Thisis
isan
anin
inline
line Perform
Perform
>>>>This
>>>>This is an in line Perform
Finished in line Perform
Finished in line Perform
>>>> This is an out of line Perform
>>>> This is an out of line Perform
>>>> This is an out of line Perform
>>>> This is an out of line Perform
>>>> This is an out of line Perform
>>>> This is an out of line Perform
>>>> This is an out of line Perform
>>>> This is an out of line Perform
>>>> This is an out of line Perform
>>>> This is an out of line Perform
Back in Begin. About to Stop
Back in Begin. About to Stop
PROCEDURE
PROCEDURE DIVISION.
DIVISION.
Begin.
Begin.
DISPLAY
DISPLAY "Starting
"Starting to
to run
run program"
program"
PERFORM
3
TIMES
PERFORM 3 TIMES
DISPLAY
DISPLAY ">>>>This
">>>>This is
is an
an in
in line
line Perform"
Perform"
END-PERFORM
END-PERFORM
DISPLAY
DISPLAY "Finished
"Finished in
in line
line Perform"
Perform"
PERFORM
OutOfLineEG
NumOfTimes
PERFORM OutOfLineEG NumOfTimes TIMES
TIMES
DISPLAY
"Back
in
Begin.
About
to
DISPLAY "Back in Begin. About to Stop".
Stop".
GOBACK.
GOBACK.
OutOfLineEG.
OutOfLineEG.
DISPLAY
DISPLAY ">>>>
">>>> This
This is
is an
an out
out of
of line
line Perform".
Perform".
Format 3 Syntax
THRU
BEFORE
EndProc WITH TEST
PERFORM 1stProc
THROUGH
AFTER
UNTIL Condition
PERFORM
PERFORMWITH
WITH
TEST
TESTBEFORE
BEFORE==
WHILE
WHILE...
...DO
DO
PERFORM
PERFORMWITH
WITH
TEST
TESTAFTER
AFTER==
REPEAT
REPEAT...
...UNTIL
UNTIL
Loop Body
test
False
True
Next Statement
Loop Body
test
False
True
Next Statement
Algorithm Template
READ StudentRecords
AT END MOVE HIGH-VALUES TO StudentRecord
END-READ
PERFORM UNTIL StudentRecord = HIGH-VALUES
DISPLAY StudentRecord
READ StudentRecords
AT END MOVE HIGH-VALUES TO StudentRecord
END-READ
END-PERFORM
RUN OF SeqRead
9456789
9456789 COUGHLANMS
COUGHLANMS LM51
LM51
9367892
TG
9367892 RYAN
RYAN
TG LM60
LM60
9368934
9368934 WILSON
WILSON HR
HR LM61
LM61
PROCEDURE
PROCEDURE DIVISION.
DIVISION.
Begin.
Begin.
OPEN
OPEN INPUT
INPUT StudentFile
StudentFile
READ
READ StudentFile
StudentFile
AT
AT END
END MOVE
MOVE HIGH-VALUES
HIGH-VALUES TO
TO StudentDetails
StudentDetails
END-READ
END-READ
PERFORM
PERFORM UNTIL
UNTIL StudentDetails
StudentDetails == HIGH-VALUES
HIGH-VALUES
DISPLAY
StudentId
SPACE
StudentName
DISPLAY StudentId SPACE StudentName SPACE
SPACE CourseCode
CourseCode
READ
StudentFile
READ StudentFile
AT
AT END
END MOVE
MOVE HIGH-VALUES
HIGH-VALUES TO
TO StudentDetails
StudentDetails
END-READ
END-READ
END-PERFORM
END-PERFORM
CLOSE
CLOSE StudentFile
StudentFile
GOBACK.
GOBACK.
Tables
and
PERFORM..VARYING
TaxTotal
Variable = Named location in memory
PAYENum CountyNum
The program to
calculate the total
taxes paid for the
country is easy to
write.
BUT.
What do we do if we
want to calculate the
taxes paid in each
county?
TaxPaid
PROCEDURE DIVISION.
Begin.
OPEN INPUT TaxFile
READ TaxFile
AT END SET EndOfTaxFile TO TRUE
END-READ
PERFORM UNTIL EndOfTaxFile
ADD TaxPaid TO TaxTotal
READ TaxFile
AT END SET EndOfTaxFile TO TRUE
END-READ
END-PERFORM.
DISPLAY "Total taxes are ", TaxTotal
CLOSE TaxFile
GOBACK.
County1
TaxTotal
County2
TaxTotal
County3
TaxTotal
County4
TaxTotal
County5
TaxTotal
PROCEDURE DIVISION.
Begin.
OPEN INPUT TaxFile
READ TaxFile
AT END SET EndOfTaxFile TO TRUE
END-READ
PERFORM SumCountyTaxes UNTIL EndOfTaxFile
DISPLAY "County 1 total is ", County1TaxTotal
: 24 Statements
: 24 Statements
Tables/Arrays
AAtable
tableis
isaacontiguous
contiguoussequence
sequenceof
ofmemory
memorylocations
locations
called
name
calledelements
elements, ,which
whichall
allhave
havethe
thesame
samename,
name,
andare
are
name and
uniquely
uniquelyidentified
identifiedby
bythat
thatname
nameand
andby
bytheir
theirposition
positionin
in
the
thesequence.
sequence.
CountyTax
10
1
MOVE 10 TO CountyTax(5)
ADD TaxPaid TO CountyTax(CountyNum)
ADD TaxPaid TO CountyTax(CountyNum + 2)
Tables/Arrays
AAtable
tableis
isaacontiguous
contiguoussequence
sequenceof
ofmemory
memorylocations
locations
called
name
calledelements
elements, ,which
whichall
allhave
havethe
thesame
samename,
name,
andare
are
name and
uniquely
uniquelyidentified
identifiedby
bythat
thatname
nameand
andby
bytheir
theirposition
positionin
in
the
thesequence.
sequence.
CountyTax
10
55
1
MOVE 10 TO CountyTax(5)
55
Tables/Arrays
AAtable
tableis
isaacontiguous
contiguoussequence
sequenceof
ofmemory
memorylocations
locations
called
name
calledelements
elements, ,which
whichall
allhave
havethe
thesame
samename,
name,
andare
are
name and
uniquely
uniquelyidentified
identifiedby
bythat
thatname
nameand
andby
bytheir
theirposition
positionin
in
the
thesequence.
sequence.
CountyTax
55
1
55
10
MOVE 10 TO CountyTax(5)
ADD TaxPaid TO CountyTax(CountyNum)
55
Tables/Arrays
AAtable
tableis
isaacontiguous
contiguoussequence
sequenceof
ofmemory
memorylocations
locations
called
name
calledelements
elements, ,which
whichall
allhave
havethe
thesame
samename,
name,
andare
are
name and
uniquely
uniquelyidentified
identifiedby
bythat
thatname
nameand
andby
bytheir
theirposition
positionin
in
the
subscript
thesequence.
sequence.The position index is called a subscript.
CountyTax
55
1
55
3
MOVE 10 TO CountyTax(5)
10
5
Subscript
CountyTax
PROCEDURE DIVISION.
Begin.
OPEN INPUT TaxFile
READ TaxFile
AT END SET EndOfTaxFile TO TRUE
END-READ
PERFORM UNTIL EndOfTaxFile
ADD TaxPaid TO CountyTax(CountyNum)
READ TaxFile
AT END SET EndOfTaxFile TO TRUE
END-READ
Subscript
END-PERFORM.
PERFORM VARYING Idx FROM 1 BY 1
UNTIL Idx GREATER THAN 26
DISPLAY "County ", CountyNum
" tax total is " CountyTax(Idx)
END-PERFORM
CLOSE TaxFile
9 Statements
GOBACK.
TaxRecord.
PAYENum
CountyName
TaxPaid
A-89432
CLARE
7894.55
CountyTax
IF CountyName = "CARLOW"
ADD TaxPaid TO CountyTax(1)
END-IF
IF CountyName = "CAVAN"
ADD TaxPaid TO CountyTax(2)
END-IF
:
:
:
:
:
:
:
:
24 TIMES
:
:
TaxRecord.
PAYENum
A-89432
CountyName
CLARE
TaxPaid
Idx
7894.55
County
CARLOW
CAVAN
CLARE
CORK
1000.00
3
745.55
4
DONEGAL DUBLIN
5
CountyTax
500.50
1
125.75
2
345.23
5
123.45
6
TaxRecord.
PAYENum
A-89432
CountyName
CLARE
TaxPaid
Idx
7894.55
County
CARLOW
CAVAN
CLARE
CORK
1000.00
3
745.55
4
DONEGAL DUBLIN
5
CountyTax
500.50
1
125.75
2
345.23
5
123.45
6
TaxRecord.
PAYENum
A-89432
CountyName
CLARE
TaxPaid
Idx
7894.55
County
CARLOW
CAVAN
CLARE
CORK
1000.00
3
745.55
4
DONEGAL DUBLIN
5
CountyTax
500.50
1
125.75
2
345.23
5
123.45
6
TaxRecord.
PAYENum
A-89432
CountyName
CLARE
TaxPaid
Idx
7894.55
County
CARLOW
CAVAN
CLARE
CORK
8894.55
3
745.55
4
DONEGAL DUBLIN
5
CountyTax
500.50
1
125.75
2
345.23
5
123.45
6
Declaring Tables.
TaxTotals
CountyTax
000000 000000
1
2
01
000000
3
TaxTotals.
02 CountyTax
000000
4
000000 000000
5
6
PIC 9(10)V99
OCCURS 26 TIMES.
or
02
e.g.
CountyTax
OCCURS 26 TIMES
PIC 9(10)V99.
TaxTotals
000000
CountyTax
67
3
000000
PayerCount
CountyTaxDetails
01
TaxTotals.
02 CountyTaxDetails
03 CountyTax
03 PayerCount
e.g.
OCCURS 26 TIMES.
PIC 9(10)V99.
PIC 9(7).
MOVE 25 TO PayerCount(2).
MOVE 67 TO CountyTax(5).
MOVE ZEROS TO CountyTaxDetails(3).
PERFORM..VARYING Syntax
THRU
BEFORE
PERFORM 1stProc
EndProc
WITH
TEST
THROUGH
AFTER
Identifier 2
Identifer1
VARYING
FROM
IndexName
2
IndexName1
Literal
Identifier3
BY
UNTIL Condition1
Literal
AFTER
Identifier 5
Identifier4
FROM
IndexName
4
IndexName3
Literal
Identifier6
BY
UNTIL
Condition2
Literal
Idx1 = 3
True
False
Loop Body
Inc Idx1
Next Statement
Idx1 = 3
True
False
Loop Body
Inc Idx1
Next Statement
Idx1 = 3
True
Next Statement
False
Loop Body
Inc Idx1
Idx1 = 3
True
Next Statement
False
Loop Body
Inc Idx1
Idx1 = 3
True
Next Statement
False
Loop Body
Inc Idx1
Idx1 = 3
True
Next Statement
False
Loop Body
Inc Idx1
1
2
Idx1 = 3
True
Next Statement
False
Loop Body
Inc Idx1
1
2
Idx1 = 3
True
Next Statement
False
Loop Body
Inc Idx1
1
2
Exit value = 3
Idx1
Idx1
Idx2
Idx2
FROM 1 BY 2
EQUAL TO 5
FROM 6 BY -1
LESS THAN 4
Move 1 to Idx1
Move 6 to Idx2
Idx1 = 5
T Idx1 Idx2
Next Statement
N
Y
Idx2 < 4
N
IterationCount
Dec Idx2
Move 6 to Idx2
Inc Idx1
x =5
=6
Advanced Tables.
01 JeansTable.
01 JeansTable.
02 Province OCCURS 4 TIMES.
03 SalesValue
PIC 9(8)V99.
03 NumSold
PIC 9(7).
01 JeansTable.
02 Province OCCURS 4 TIMES.
03 SalesValue
PIC 9(8)V99.
03 NumSold
PIC 9(7).
12346.99
309
Province
SalesValue
NumSold
01 JeansTable.
02 Province OCCURS 4 TIMES.
1
1
2
2
3
2
4
2
01 JeansTable.
02 Province OCCURS 4 TIMES.
03 Gender OCCURS 2 TIMES.
04 SalesValue
PIC 9(8)V99.
04 NumSold
PIC 9(7).
01 JeansTable.
02 Province OCCURS 4 TIMES.
2
2
3
2
4
2
01 JeansTable.
02 Province OCCURS 4 TIMES.
03 Gender OCCURS 2 TIMES.
2
2
3
2
4
2
1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3
01 JeansTable.
02 Province OCCURS 4 TIMES.
03 Gender OCCURS 2 TIMES.
04 Colour OCCURS 3 TIMES.
2
2
3
2
4
2
1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3
01 JeansTable.
02 Province OCCURS 4 TIMES.
03 Gender OCCURS 2 TIMES.
04 Colour OCCURS 3 TIMES.
05 SalesValue PIC 9(8)V99.
05 NumSold
PIC 9(7).
12346.99
SalesValue
309
Colour
NumSold
Record Elements.
1
1
2
2
1 2 3 1 2 3
3
2
1 2 3 1 2 3
4
2
1 2 3 1 2 3
1 2 3 1 2 3
01 JeansTable.
02 Province OCCURS 4 TIMES.
03 ProviceTotal PIC 9(8).
03 Gender OCCURS 2 TIMES.
04 Colour OCCURS 3 TIMES.
05 SalesValue PIC 9(8)V99.
05 NumSold
PIC 9(7).
.3
01 Rates.
02 10Rate
PIC 99V999.
02 100Rate REDEFINES 10Rate PIC 999V99.
02 1000Rate REDEFINES 10Rate PIC 9999V9.
.4
01 Rates.
02 10Rate
PIC 99V999.
02 100Rate REDEFINES 10Rate PIC 999V99.
02 1000Rate REDEFINES 10Rate PIC 9999V9.
.5
01 Rates.
02 10Rate
PIC 99V999.
02 100Rate REDEFINES 10Rate PIC 999V99.
02 1000Rate REDEFINES 10Rate PIC 9999V9.
HoldDate
11
06
USDay USMonth
USDate
1983
USYear
01 HoldDate.
02 EuroDate.
03 EuroDay PIC 99.
03 EuroMonth PIC 99.
03 EuroYear PIC 9(4).
02 USDate REDEFINES EuroDate.
03 USMonth PIC 99.
03 USDay
PIC 99.
03 USYear
PIC 9(4).
01 LetterTable.
02 TableValues.
01 LetterTable.
02 TableValues.
03 FILLER PIC X(13)
VALUE "ABCDEFGHIJKLM".
03 FILLER PIC X(13)
VALUE "NOPQRSTUVWXYZ".
01 LetterTable.
02 TableValues.
03 FILLER PIC X(13)
VALUE "ABCDEFGHIJKLM".
03 FILLER PIC X(13)
VALUE "NOPQRSTUVWXYZ".
02 FILLER REDEFINES TableValues.
03 Letter PIC X OCCURS 26 TIMES.
50 75
90
75 85 95
35 43 65 40 60 85
01 BonusTable.
02 BonusValues.
03 FILLER PIC X(24)
VALUE "507590758595354365406085".
50 75
90
75 85 95
35 43 65 40 60 85
01 BonusTable.
02 BonusValues.
03 FILLER PIC X(24)
VALUE "507590758595354365406085".
02 FILLER REDEFINES BonusValues.
03 Province OCCURS 4 TIMES.
50 75
2
3
90
3
3
75 85 95
4
3
35 43 65 40 60 85
01 BonusTable.
02 BonusValues.
03 FILLER PIC X(24)
VALUE "507590758595354365406085".
02 FILLER REDEFINES BonusValues.
03 Province OCCURS 4 TIMES.
04 Bonus OCCURS 3 TIMES PIC 99.
01 TaxTable.
02 County OCCURS 32 TIMES.
03 CountyTax PIC 9(5) VALUE ZEROS.
03 CountyName PIC X(12) VALUE SPACES.
Searching Tables.
01
01 LetterTable.
LetterTable.
02
02 TableValues.
TableValues.
03
03 FILLER
FILLER PIC
PIC X(13)
X(13)
VALUE
ABCDEFGHIJKLM
VALUE ""ABCDEFGHIJKLM".
ABCDEFGHIJKLM".
ABCDEFGHIJKLM
03
03 FILLER
FILLER PIC
PIC X(13)
X(13)
VALUE
NOPQRSTUVWXYZ
VALUE ""NOPQRSTUVWXYZ".
NOPQRSTUVWXYZ".
NOPQRSTUVWXYZ
01
01 LetterTable.
LetterTable.
02
02 TableValues.
TableValues.
03
03 FILLER
FILLER PIC
PIC X(13)
X(13)
VALUE
ABCDEFGHIJKLM
VALUE ""ABCDEFGHIJKLM".
ABCDEFGHIJKLM".
ABCDEFGHIJKLM
03
03 FILLER
FILLER PIC
PIC X(13)
X(13)
VALUE
NOPQRSTUVWXYZ
VALUE ""NOPQRSTUVWXYZ".
NOPQRSTUVWXYZ".
NOPQRSTUVWXYZ
02
02 FILLER
FILLER REDEFINES
REDEFINES TableValues.
TableValues.
03
03 Letter
Letter PIC
PIC XX OCCURS
OCCURS 26
26 TIMES.
TIMES.
Searching a Table
A
10
11
12
01
01 LetterTable.
LetterTable.
02
02 TableValues.
TableValues.
03
03 FILLER
FILLER PIC
PIC X(13)
X(13)
VALUE
VALUE "ABCDEFGHIJKLM".
"ABCDEFGHIJKLM".
03
03 FILLER
FILLER PIC
PIC X(13)
X(13)
VALUE
VALUE "NOPQRSTUVWXYZ".
"NOPQRSTUVWXYZ".
02
FILLER
REDEFINES
02 FILLER REDEFINES TableValues.
TableValues.
03
Letter
PIC
X
OCCURS
03 Letter PIC X OCCURS 26
26 TIMES.
TIMES.
PERFORM
PERFORM VARYING
VARYING Idx
Idx FROM
FROM 11 BY
BY 11 UNTIL
UNTIL
LetterIn
LetterIn EQUAL
EQUAL TO
TO Letter(Idx)
Letter(Idx)
END-PERFORM.
END-PERFORM.
DISPLAY
DISPLAY LetterIn,
LetterIn, "is
"is in
in position
position ",
", Idx.
Idx.
Search Syntax
SET Syntax
Searching a Table
A
10
11
12
01
01 LetterTable.
LetterTable.
02
02 TableValues.
TableValues.
03
03 FILLER
FILLER PIC
PIC X(13)
X(13)
VALUE
"ABCDEFGHIJKLM".
VALUE "ABCDEFGHIJKLM".
03
FILLER
03 FILLER PIC
PIC X(13)
X(13)
VALUE
VALUE "NOPQRSTUVWXYZ".
"NOPQRSTUVWXYZ".
02
02 FILLER
FILLER REDEFINES
REDEFINES TableValues.
TableValues.
03
03 Letter
Letter PIC
PIC XX OCCURS
OCCURS 26
26 TIMES
TIMES
INDEXED
INDEXED BY
BY LetterIdx.
LetterIdx.
SET
SET LetterIdx
LetterIdx TO
TO 1.
1.
SEARCH
Letter
SEARCH Letter
AT
AT END
END DISPLAY
DISPLAY "Letter
"Letter
WHEN
WHEN Letter(LetterIdx)
Letter(LetterIdx)
DISPLAY
DISPLAY LetterIn,
LetterIn,
END-SEARCH.
END-SEARCH.
not
not found!"
found!"
== LetterIn
LetterIn
"is
"is in
in position
position ",
", Idx
Idx
1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4
01 TimeTable.
02 Day OCCURS 5 TIMES INDEXED BY DayIdx.
03 Hours OCCURS 8 TIMES INDEXED BY HourIdx.
04 Item
PIC X(10).
04 Location
PIC X(10).
SET DayIdx TO 0.
PERFORM UNTIL MeetingFound OR DayIdx > 5
SET DayIdx UP BY 1
SET HourIdx TO 1
SEARCH Hours WHEN MeetingType = Item(DayIdx, HourIdx)
SET MeetingFound TO TRUE
DISPLAY MeetingType " on " DayIdx " at " HourIdx
END-SEARCH
END-PERFORM.
10
11
12
01
01 LetterTable.
LetterTable.
02
02 TableValues.
TableValues.
03
03 FILLER
FILLER PIC
PIC X(13)
X(13)
VALUE
VALUE "ABCDEFGHIJKLM".
"ABCDEFGHIJKLM".
03
03 FILLER
FILLER PIC
PIC X(13)
X(13)
VALUE
VALUE "NOPQRSTUVWXYZ".
"NOPQRSTUVWXYZ".
02
FILLER
REDEFINES
02 FILLER REDEFINES TableValues.
TableValues.
03
Letter
PIC
X
OCCURS
03 Letter PIC X OCCURS 26
26 TIMES
TIMES
ASCENDING
KEY
ASCENDING KEY IS
IS Letter
Letter
INDEXED
BY
LetterIdx.
INDEXED BY LetterIdx.
SEARCH
SEARCH ALL
ALL Letter
Letter
WHEN
WHEN Letter(LetterIdx)
Letter(LetterIdx) == LetterIn
LetterIn
DISPLAY
LetterIn,
"is
DISPLAY LetterIn, "is in
in position
position ",
", Idx
Idx
END-SEARCH.
END-SEARCH.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
Lower
Upper
Middle
26
13
ALGORITHM.
Letter(Middle)
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
Lower
Upper
Middle
14
26
13
ALGORITHM.
Letter(Middle)
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
Lower
Upper
Middle
14
26
20
ALGORITHM.
Letter(Middle)
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
Lower
Upper
Middle
14
19
20
ALGORITHM.
Letter(Middle)
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
Lower
Upper
Middle
14
19
16
ALGORITHM.
Letter(Middle)
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
Lower
Upper
Middle
17
19
16
ALGORITHM.
Letter(Middle)
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
Lower
Upper
Middle
17
19
18
ALGORITHM.
Letter(Middle)
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
Lower
Upper
Middle
17
17
18
ALGORITHM.
Letter(Middle)
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
Lower
Upper
Middle
17
17
18
ALGORITHM.
Letter(Middle)
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
Lower
Upper
Middle
17
17
18
ALGORITHM.
Letter(Middle)
Post
Post Codes
Codes and
and Names
Names
03
03 FILLER
FILLER PIC
PIC X(20)
X(20) VALUE
VALUE ??????????????
??????????????
02
02 FILLER
FILLER REDEFINES
REDEFINES StateValues.
StateValues.
03
03 States
States OCCURS
OCCURS 50
50 TIMES
TIMES
ASCENDING
ASCENDING KEY
KEY IS
IS StateName
StateName
INDEXED
INDEXED BY
BY StateIdx.
StateIdx.
04
PIC
04 PostCode
PostCode
PIC X(6).
X(6).
04
StateName
PIC
X(14).
04 StateName
PIC X(14).
SEARCH
SEARCH ALL
ALL States
States
AT
AT END
END DISPLAY
DISPLAY "State
"State not
not found"
found"
WHEN
WHEN StateName(StateIdx)
StateName(StateIdx) == InputName
InputName
MOVE
MOVE PostCode(StateIdx)
PostCode(StateIdx) TO
TO PrintPostCode
PrintPostCode
END-SEARCH.
END-SEARCH.
Introduction
to
Sequential Files
COBOL's forte
10091961
10091961
31121976
31121976
23061964
23061964
03111979
03111979
12121976
12121976
DATA
DATA DIVISION.
DIVISION.
FILE
SECTION.
FILE SECTION.
FD
FD StudentFile.
StudentFile.
01
StudentDetails.
01 StudentDetails.
02
PIC
02 StudId
StudId
PIC 9(7).
9(7).
02
StudName
PIC
X(8).
02 StudName
PIC X(8).
02
DateOfBirth
PIC
02 DateOfBirth PIC X(8).
X(8).
occurrences
Record Type
(Template)
(Structure)
Record Buffers
Record Buffers
Program
DISK
STUDENTS.DAT
Record Instance
IDENTIFICATION DIVISION.
etc.
ENVIRONMENT DIVISION.
etc.
DATA DIVISION.
FILE SECTION.
RecordBuffer
Declaration
Implications of Buffers
02
Student
StudentId.
Id.
02 StudentId
StudentId
Student
StudentName.
Name.
Surname
Surname
Initials
Initials
Date
Dateof
ofBirth
Birth
Year
YearofofBirth
Birth
Month
MonthofofBirth
Birth
Day
DayofofBirth
Birth
Course
CourseCode
Code
Value of grant
Value of grant
Gender
Gender
PIC
PIC 9(7).
9(7).
02
02 StudentName.
StudentName.
03
03 Surname
SurnamePIC
PIC X(8).
X(8).
03
PIC
03 Initials
Initials
PIC XX.
XX.
02
02 DateOfBirth.
DateOfBirth.
03
03 YOBirth
YOBirthPIC
PIC 99.
99.
03
03 MOBirth
MOBirthPIC
PIC 99.
99.
03
03 DOBirth
DOBirthPIC
PIC 99.
99.
02
02 CourseCode
CourseCode PIC
PIC X(4).
X(4).
02
02 Grant
Grant PIC
PIC 9(4).
9(4).
02
02 Gender
Gender PIC
PIC X.
X.
PIC
PIC 9(7).
9(7).
PIC
X(8).
PIC
X(8).
PIC
XX.
PIC XX.
PIC
9(2).
PIC
9(2).
PIC
9(2).
PIC 9(2).
9(2).
PIC
PIC X(4).
9(2).
PIC
PIC 9(4).
X(4).
PIC
PIC X.
9(4).
PIC
PIC X.
DISK
STUDENTS.DAT
ENVIRONMENT
DIVISION.
ENVIRONMENT
DIVISION.
INPUT-OUTPUT
SECTION.
INPUT-OUTPUT
SECTION.
FILE-CONTROL.
FILE-CONTROL.
SELECT
StudentFile
SELECT
StudentFile
ASSIGN
ASSIGN TO
TO STUDENTS.DAT.
STUDENTS.DAT.
DATA
DIVISION.
DATA
DIVISION.
FILE
SECTION.
FILE
SECTION.
FD
StudentFile.
FD StudentDetails.
StudentFile.
01
01 02
StudentDetails.
StudentId
PIC
9(7).
02
StudentId
PIC
9(7).
02
StudentName.
02 03
StudentName.
Surname
PIC
X(8).
03
Surname
PIC
X(8).
03
Initials
PIC
XX.
03 Initials
PIC XX.
02
DateOfBirth.
02 03
DateOfBirth.
YOBirth
PIC
9(2).
03
YOBirth
PIC
9(2).
03
MOBirth
PIC
9(2).
03 DOBirth
MOBirth
PIC 9(2).
9(2).
03
PIC
03 DOBirth
PIC X(4).
9(2).
02
CourseCode
PIC
02 Grant
CourseCode
PIC 9(4).
X(4).
02
PIC
02 Gender
Grant
PIC X.
9(4).
02
PIC
02 Gender
PIC X.
[ORGANIZATION IS
SEQUENTIAL].
RECORD
OPEN
Before your program can access the data in an input file or
place data in an output file you must make the file available
to the program by OPENing it.
READ
The READ copies a record occurrence/instance from the
file and places it in the record buffer.
WRITE
The WRITE copies the record it finds in the record buffer
to the file.
CLOSE
You must ensure that (before terminating) your program
closes all the files it has opened. Failure to do so may result
in data not being written to the file or users being prevented
from accessing the file.
INTO Identifier
AT END StatementBlock
END - READ
9
9
9
9
3
3
3
3
3
8
4
7
4
3
7
8
5
7
2
8
Course.
StudentName
6
1
9
1
7
5
2
1
F r a n k
F
T
T
B
r
h
o
i
a
o
n
l
C u r t a i n
n k
C u
ma s
H
y
O B
l y
D o
r
e
r
w
t
a
i
n
a
l
a
e
i n
y
n
s
L M 0 5 1
L
L
L
L
EOF
PERFORM UNTIL StudentRecord = HIGH-VALUES
READ StudentRecords
AT END MOVE HIGH-VALUES TO StudentRecord
END-READ
END-PERFORM.
M
M
M
M
0
0
0
0
5
6
5
2
1
8
1
1
9
9
9
9
3
3
3
3
3
8
4
7
4
3
7
8
5
7
2
8
Course.
StudentName
6
1
9
1
7
5
2
1
T h o ma s
F
T
T
B
r
h
o
i
a
o
n
l
H e a l y
n k
C u
ma s
H
y
O B
l y
D o
r
e
r
w
t
a
i
n
a
l
a
e
i n
y
n
s
L M 0 6 8
L
L
L
L
EOF
PERFORM UNTIL StudentRecord = HIGH-VALUES
READ StudentRecords
AT END MOVE HIGH-VALUES TO StudentRecord
END-READ
END-PERFORM.
M
M
M
M
0
0
0
0
5
6
5
2
1
8
1
1
9
9
9
9
3
3
3
3
3
8
4
7
4
3
7
8
5
7
2
8
Course.
StudentName
6
1
9
1
7
5
2
1
T o n y
F
T
T
B
r
h
o
i
a
o
n
l
O B r i a n
n k
C u
ma s
H
y
O B
l y
D o
r
e
r
w
t
a
i
n
a
l
a
e
i n
y
n
s
L M 0 5 1
L
L
L
L
EOF
PERFORM UNTIL StudentRecord = HIGH-VALUES
READ StudentRecords
AT END MOVE HIGH-VALUES TO StudentRecord
END-READ
END-PERFORM.
M
M
M
M
0
0
0
0
5
6
5
2
1
8
1
1
9
9
9
9
3
3
3
3
3
8
4
7
4
3
7
8
5
7
2
8
Course.
StudentName
6
1
9
1
7
5
2
1
B i l l y
F
T
T
B
r
h
o
i
a
o
n
l
n
m
y
l
k
a s
O
y
D o w n e s
C u
H
B
D o
r
e
r
w
t
a
i
n
a
l
a
e
i n
y
n
s
L M 0 2 1
L
L
L
L
EOF
PERFORM UNTIL StudentRecord = HIGH-VALUES
READ StudentRecords
AT END MOVE HIGH-VALUES TO StudentRecord
END-READ
END-PERFORM.
M
M
M
M
0
0
0
0
5
6
5
2
1
8
1
1
Course.
StudentName
HIGH-VALUES
9
9
9
9
3
3
3
3
3
8
4
7
4
3
7
8
5
7
2
8
6
1
9
1
7
5
2
1
F
T
T
B
r
h
o
i
a
o
n
l
n k
C u
ma s
H
y
O B
l y
D o
r
e
r
w
t
a
i
n
a
l
a
e
i n
y
n
s
L
L
L
L
EOF
PERFORM UNTIL StudentRecord = HIGH-VALUES
READ StudentRecords
AT END MOVE HIGH-VALUES TO StudentRecord
END-READ
END-PERFORM.
M
M
M
M
0
0
0
0
5
6
5
2
1
8
1
1
WRITE Syntax.
WRITE RecordName FROM Identifier
LINE
AdvanceNum
LINES
BEFORE
ADVANCING MnemonicName
AFTER
PAGE
StudentRecord
StudentID
9 3 3 4 5 6 7
StudentName
F r a n k
C u r t a i n
Course.
L M 0 5 1
Students.Dat
9 3 3 4 5 6 7 F r a n k
EO
F
C u r t a i n
L M 0 5 1
StudentRecord
StudentID
9 3 8 3 7 1 5
StudentName
T h o ma s
H e a l y
Course.
L M 0 6 8
Students.Dat
9 3 3 4 5 6 7 F r a n k
C u r t a i n
9 3 8 3 7 1 5 T h o ma s
H e a l y
EO
F
L M 0 5 1
L M 0 6 8
$ SET SOURCEFORMAT"FREE"
$ SET SOURCEFORMAT"FREE"
IDENTIFICATION
DIVISION.
IDENTIFICATION
DIVISION.
PROGRAM-ID. SeqWrite.
PROGRAM-ID.
SeqWrite.
AUTHOR. Michael Coughlan.
AUTHOR. Michael Coughlan.
ENVIRONMENT DIVISION.
ENVIRONMENT SECTION.
DIVISION.
INPUT-OUTPUT
INPUT-OUTPUT
FILE-CONTROL. SECTION.
FILE-CONTROL.
SELECT StudentFile ASSIGN TO "STUDENTS.DAT"
SELECT
StudentFile
TO "STUDENTS.DAT"
ORGANIZATION
IS ASSIGN
LINE SEQUENTIAL.
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
DATASECTION.
DIVISION.
FILE
FILE
SECTION.
FD
StudentFile.
FD
StudentFile.
01 StudentDetails.
0102
StudentDetails.
StudentId
PIC 9(7).
02
StudentId
PIC 9(7).
02 StudentName.
02 03
StudentName.
Surname
PIC X(8).
03Initials
Surname
PICXX.
X(8).
03
PIC
03
Initials
PIC
XX.
02 DateOfBirth.
02 03
DateOfBirth.
YOBirth
PIC 9(2).
03MOBirth
YOBirth
PIC9(2).
9(2).
03
PIC
03
MOBirth
PIC
9(2).
03 DOBirth
PIC 9(2).
03
DOBirth
PIC
9(2).
02 CourseCode
PIC X(4).
02
CourseCode
PIC
X(4).
02 Grant
PIC 9(4).
02
Grant
PIC
9(4).
02 Gender
PIC X.
02 Gender
PIC X.
PROCEDURE DIVISION.
PROCEDURE DIVISION.
Begin.
Begin.
OPEN OUTPUT StudentFile.
OPEN OUTPUT
StudentFile.
DISPLAY
"Enter
student details using template below. Enter no data to end.".
DISPLAY
"Enter
student details using template below. Enter no data to end.".
PERFORM GetStudentDetails.
PERFORM
GetStudentDetails.
PERFORM UNTIL StudentDetails = SPACES
PERFORM
UNTIL StudentDetails = SPACES
WRITE StudentDetails
WRITE
PERFORM StudentDetails
GetStudentDetails
PERFORM GetStudentDetails
END-PERFORM.
END-PERFORM.
CLOSE
StudentFile.
CLOSE
StudentFile.
STOP
RUN.
STOP RUN.
GetStudentDetails.
GetStudentDetails.
DISPLAY "NNNNNNNSSSSSSSSIIYYMMDDCCCCGGGGS".
DISPLAYStudentDetails.
"NNNNNNNSSSSSSSSIIYYMMDDCCCCGGGGS".
ACCEPT
ACCEPT StudentDetails.
$ SET SOURCEFORMAT"FREE"
$ SET SOURCEFORMAT"FREE"
IDENTIFICATION
DIVISION.
IDENTIFICATION
DIVISION.
PROGRAM-ID. SeqRead.
PROGRAM-ID.
SeqRead.
AUTHOR. Michael Coughlan.
AUTHOR. Michael Coughlan.
ENVIRONMENT DIVISION.
ENVIRONMENT SECTION.
DIVISION.
INPUT-OUTPUT
INPUT-OUTPUT
FILE-CONTROL. SECTION.
FILE-CONTROL.
SELECT StudentFile ASSIGN TO STUDENTS.DAT
SELECT
StudentFile
TO STUDENTS.DAT
ORGANIZATION
IS ASSIGN
LINE SEQUENTIAL.
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
DATASECTION.
DIVISION.
FILE
FILE
SECTION.
FD StudentFile.
FD
StudentFile.
01 StudentDetails.
0102
StudentDetails.
StudentId
PIC 9(7).
02 StudentName.
StudentId
PIC 9(7).
02
02 03
StudentName.
Surname
PIC X(8).
03Initials
Surname
PICXX.
X(8).
03
PIC
03
Initials
PIC
XX.
02 DateOfBirth.
02 03
DateOfBirth.
YOBirth
PIC 9(2).
03MOBirth
YOBirth
PIC9(2).
9(2).
03
PIC
03
MOBirth
PIC
9(2).
03 DOBirth
PIC 9(2).
03 DOBirth
PICX(4).
9(2).
02 CourseCode
PIC
02 Grant
CourseCode
PIC9(4).
X(4).
02
PIC
02
Grant
PIC
9(4).
02 Gender
PIC X.
02 Gender
PIC X.
PROCEDURE DIVISION.
PROCEDURE DIVISION.
Begin.
Begin.
OPEN INPUT StudentFile
OPENStudentFile
INPUT StudentFile
READ
READ
StudentFile
AT END
MOVE HIGH-VALUES TO StudentDetails
AT END MOVE HIGH-VALUES TO StudentDetails
END-READ
END-READ
PERFORM
UNTIL StudentDetails = HIGH-VALUES
PERFORMStudentId
UNTIL StudentDetails
= HIGH-VALUES
DISPLAY
SPACE StudentName
SPACE CourseCode
DISPLAY
StudentId
SPACE
StudentName
SPACE CourseCode
READ StudentFile
READ
StudentFile
AT END MOVE HIGH-VALUES TO StudentDetails
AT END MOVE HIGH-VALUES TO StudentDetails
END-READ
END-READ
END-PERFORM
END-PERFORM
CLOSE
StudentFile
CLOSE
StudentFile
STOP
RUN.
STOP RUN.
Processin
g
Sequential
Files
$ SET SOURCEFORMAT"FREE"
$ SET SOURCEFORMAT"FREE"
IDENTIFICATION
DIVISION.
IDENTIFICATION
DIVISION.
PROGRAM-ID. SeqWrite.
PROGRAM-ID.
SeqWrite.
Enter student details using template below. AUTHOR.
Press CRMichael
to end.
Coughlan.
Enter
student
details
using
template
below.
Press
CR
to
end.
AUTHOR.
Michael
Coughlan.
NNNNNNNSSSSSSSSIIYYMMDDCCCCGGGGS
NNNNNNNSSSSSSSSIIYYMMDDCCCCGGGGS
ENVIRONMENT DIVISION.
9456789COUGHLANMS580812LM510598M
ENVIRONMENT SECTION.
DIVISION.
INPUT-OUTPUT
9456789COUGHLANMS580812LM510598M
NNNNNNNSSSSSSSSIIYYMMDDCCCCGGGGS
INPUT-OUTPUT
SECTION.
FILE-CONTROL.
NNNNNNNSSSSSSSSIIYYMMDDCCCCGGGGS
FILE-CONTROL.
9367892RYAN
TG521210LM601222F
SELECT StudentFile ASSIGN TO "STUDENTS.DAT"
9367892RYAN
TG521210LM601222F
SELECT
StudentFile
TO "STUDENTS.DAT"
NNNNNNNSSSSSSSSIIYYMMDDCCCCGGGGS
ORGANIZATION
IS ASSIGN
LINE SEQUENTIAL.
NNNNNNNSSSSSSSSIIYYMMDDCCCCGGGGS
ORGANIZATION
IS
LINE
SEQUENTIAL.
9368934WILSON HR520323LM610786M
9368934WILSON HR520323LM610786M
DATA DIVISION.
NNNNNNNSSSSSSSSIIYYMMDDCCCCGGGGS
DATASECTION.
DIVISION.
FILE
NNNNNNNSSSSSSSSIIYYMMDDCCCCGGGGS
CarriageReturn
FILE
SECTION.
FD
StudentFile.
CarriageReturn
FDStudentDetails.
StudentFile.
01
0102
StudentDetails.
StudentId
PIC 9(7).
02 StudentName.
StudentId
PIC 9(7).
02
02 03
StudentName.
Surname
PIC X(8).
03Initials
Surname
PICXX.
X(8).
03
PIC
03 Initials
PIC XX.
02 DateOfBirth.
02 03
DateOfBirth.
YOBirth
PIC 9(2).
03MOBirth
YOBirth
PIC9(2).
9(2).
03
PIC
03
MOBirth
PIC
9(2).
03 DOBirth
PIC 9(2).
03 DOBirth
PICX(4).
9(2).
02 CourseCode
PIC
02 Grant
CourseCode
PIC9(4).
X(4).
02
PIC
02
Grant
PIC
9(4).
02 Gender
PIC X.
PROCEDURE DIVISION.
02 Gender
PIC X.
Run of SeqWrite
PROCEDURE DIVISION.
Begin.
Begin.
OPEN OUTPUT StudentFile
OPEN OUTPUT
StudentFile
DISPLAY
"Enter
student details using template below. Press CR to end.".
DISPLAY
"Enter
student details using template below. Press CR to end.".
PERFORM GetStudentDetails
PERFORMUNTIL
GetStudentDetails
PERFORM
StudentDetails = SPACES
PERFORM
StudentDetails = SPACES
WRITE UNTIL
StudentDetails
WRITE
StudentDetails
PERFORM GetStudentDetails
PERFORM GetStudentDetails
END-PERFORM
END-PERFORM
CLOSE StudentFile
CLOSE
StudentFile
STOP
RUN.
STOP RUN.
GetStudentDetails.
GetStudentDetails.
DISPLAY "NNNNNNNSSSSSSSSIIYYMMDDCCCCGGGGS".
DISPLAYStudentDetails.
"NNNNNNNSSSSSSSSIIYYMMDDCCCCGGGGS".
ACCEPT
ACCEPT StudentDetails.
RUN OF SeqRead
9456789 COUGHLANMS LM51
9456789 COUGHLANMS LM51
9367892 RYAN
TG LM60
9367892 RYAN
TG LM60
9368934 WILSON HR LM61
9368934 WILSON HR LM61
$ SET SOURCEFORMAT"FREE"
$ SET SOURCEFORMAT"FREE"
IDENTIFICATION
DIVISION.
IDENTIFICATION
DIVISION.
PROGRAM-ID.
SeqRead.
PROGRAM-ID.
SeqRead.
AUTHOR. Michael Coughlan.
AUTHOR. Michael Coughlan.
ENVIRONMENT DIVISION.
ENVIRONMENT SECTION.
DIVISION.
INPUT-OUTPUT
INPUT-OUTPUT SECTION.
FILE-CONTROL.
FILE-CONTROL.
SELECT StudentFile ASSIGN TO "STUDENTS.DAT"
SELECT
StudentFile
TO "STUDENTS.DAT"
ORGANIZATION
IS ASSIGN
LINE SEQUENTIAL.
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
DATASECTION.
DIVISION.
FILE
FILE
SECTION.
FD StudentFile.
FDStudentDetails.
StudentFile.
01
0102
StudentDetails.
StudentId
PIC 9(7).
02 StudentName.
StudentId
PIC 9(7).
02
02 03
StudentName.
Surname
PIC X(8).
03Initials
Surname
PICXX.
X(8).
03
PIC
03
Initials
PIC
XX.
02 DateOfBirth.
02 03
DateOfBirth.
YOBirth
PIC 9(2).
03MOBirth
YOBirth
PIC9(2).
9(2).
03
PIC
03
MOBirth
PIC
9(2).
03 DOBirth
PIC 9(2).
03
DOBirth
PIC
9(2).
02 CourseCode
PIC X(4).
02 Grant
CourseCode
PIC9(4).
X(4).
02
PIC
02 Gender
Grant
PICX.
9(4).
02
PIC
02 Gender
PIC X.
PROCEDURE DIVISION.
PROCEDURE DIVISION.
Begin.
Begin.
OPEN INPUT StudentFile
OPENStudentFile
INPUT StudentFile
READ
READ
AT StudentFile
END MOVE HIGH-VALUES TO StudentDetails
AT END MOVE HIGH-VALUES TO StudentDetails
END-READ
END-READ
PERFORM
UNTIL StudentDetails = HIGH-VALUES
PERFORM
UNTIL
StudentDetails
= HIGH-VALUES
DISPLAY
StudentId
SPACE StudentName
SPACE CourseCode
DISPLAY
StudentId
SPACE
StudentName
SPACE CourseCode
READ StudentFile
READ
AT StudentFile
END MOVE HIGH-VALUES TO StudentDetails
AT END MOVE HIGH-VALUES TO StudentDetails
END-READ
END-READ
END-PERFORM
END-PERFORM
CLOSE
StudentFile
CLOSE
StudentFile
STOP
RUN.
STOP RUN.
DATA ORGANIZATION
METHOD OF ACCESS
Data organization refers to the way the records of the file are
organized on the backing storage device.
COBOL recognizes three main file organizations;
Relative
- Relative record number based organization.
Indexed
- Index based organization.
Sequential Organization
Unordered File
RecordA
RecordA
RecordM
RecordM
RecordB
RecordB
RecordH
RecordH
RecordG
RecordG
RecordB
RecordB
RecordH
RecordH
RecordN
RecordN
RecordK
RecordK
RecordA
RecordA
RecordM
RecordM
RecordK
RecordK
RecordN
RecordN
RecordG
RecordG
PROGRAM
RecordF
RecordF
FILE
FILE SECTION.
SECTION.
PROGRAM
RecordP
RecordP
RecordW
RecordW
TFRec
UFRec
PROCEDURE
PROCEDURE DIVISION.
DIVISION.
OPEN
OPEN EXTEND
EXTEND UF.
UF.
OPEN
OPEN INPUT
INPUT TF.
TF.
READ
READ TF.
TF.
MOVE
TFRec
MOVE TFRec TO
TO UFRec.
UFRec.
WRITE
WRITE UFRec.
UFRec.
Unordered
File
RecordM
RecordM
RecordH
RecordH
RecordB
RecordB
RecordN
RecordN
RecordA
RecordA
RecordK
RecordK
RecordG
RecordG
PROGRAM
FILE
FILE SECTION.
SECTION.
PROGRAM
RecordF
RecordF
PROCEDURE
PROCEDURE DIVISION.
DIVISION.
OPEN
OPEN EXTEND
EXTEND UF.
UF.
OPEN
OPEN INPUT
INPUT TF.
TF.
READ
READ TF.
TF.
MOVE
TFRec
MOVE TFRec TO
TO UFRec.
UFRec.
WRITE
WRITE UFRec.
UFRec.
Unordered
File
RecordM
RecordM
RecordH
RecordH
RecordB
RecordB
RecordN
RecordN
RecordA
RecordA
RecordK
RecordK
RecordG
RecordG
RecordF
RecordF
Unordered
File
RecordF
RecordF
RecordP
RecordP
RecordM
RecordM
RecordW
RecordW
RecordB
RecordB
RecordH
RecordH
RecordN
RecordN
RESULT
RecordA
RecordA
RecordK
RecordK
RecordG
RecordG
RecordF
RecordF
RecordP
RecordP
RecordW
RecordW
Why?
New File
Delete UF
Record?
Unordered File
RecordM
RecordM
RecordH
RecordH
RecordB
RecordB
RecordN
RecordN
RecordA
RecordA
RecordK
RecordK
RecordM
RecordM
NO
New File
Delete UF
Record?
Unordered File
RecordM
RecordM
RecordH
RecordH
RecordB
RecordB
RecordN
RecordN
RecordA
RecordA
RecordK
RecordK
RecordM
RecordM
NO
RecordH
RecordH
New File
Delete UF
Record?
Unordered File
RecordM
RecordM
RecordH
RecordH
RecordB
RecordB
RecordN
RecordN
RecordA
RecordA
RecordK
RecordK
RecordM
RecordM
YES
RecordH
RecordH
New File
Delete UF
Record?
Unordered File
RecordM
RecordM
RecordH
RecordH
RecordB
RecordB
RecordN
RecordN
RecordA
RecordA
RecordK
RecordK
RecordM
RecordM
NO
RecordH
RecordH
RecordN
RecordN
But wait...
We should have deleted
RecordM. Too late. Its
already been written to the
new file.
PROGRAM
FILE
FILE SECTION.
SECTION.
TFRec
OFRec
NFRec
PROCEDURE
PROCEDURE DIVISION.
DIVISION.
OPEN
INPUT
OPEN INPUT TF.
TF.
OPEN
INPUT
OF
OPEN INPUT OF
OPEN
OPEN OUTPUT
OUTPUT NF.
NF.
READ
TF.
READ TF.
READ
READ OF.
OF.
IF
TFKey
IF TFKey NOT
NOT == OFKey
OFKey
MOVE
OFRec
TO
MOVE OFRec TO NFRec
NFRec
WRITE
NFRec
WRITE NFRec
READ
READ OF
OF
ELSE
ELSE
READ
READ TF
TF
READ
OF
READ OF
END-IF.
END-IF.
New File
PROGRAM
FILE
FILE SECTION.
SECTION.
RecordB
New File
RecordA
RecordA
RecordA
RecordA
PROCEDURE
PROCEDURE DIVISION.
DIVISION.
OPEN
INPUT
OPEN INPUT TF.
TF.
OPEN
INPUT
OF
OPEN INPUT OF
OPEN
OPEN OUTPUT
OUTPUT NF.
NF.
READ
TF.
READ TF.
READ
READ OF.
OF.
IF
TFRec
IF TFRec NOT
NOT == OFRec
OFRec
MOVE
OFRec
TO
MOVE OFRec TO NFRec
NFRec
WRITE
NFRec
WRITE NFRec
READ
READ OF
OF
ELSE
ELSE
READ
READ TF
TF
READ
OF
READ OF
END-IF.
END-IF.
Problem !!
How can we
recognize which
record we want to
delete?
By its Key Field
PROGRAM
FILE
FILE SECTION.
SECTION.
RecordB
RecordB
RecordA
PROCEDURE
PROCEDURE DIVISION.
DIVISION.
OPEN
INPUT
OPEN INPUT TF.
TF.
OPEN
INPUT
OF
OPEN INPUT OF
OPEN
OPEN OUTPUT
OUTPUT NF.
NF.
READ
TF.
READ TF.
READ
READ OF.
OF.
IF
TFKey
IF TFKey NOT
NOT == OFKey
OFKey
MOVE
OFRec
TO
MOVE OFRec TO NFRec
NFRec
WRITE
NFRec
WRITE NFRec
READ
READ OF
OF
ELSE
ELSE
READ
READ TF
TF
READ
OF
READ OF
END-IF.
END-IF.
New File
RecordA
RecordA
PROGRAM
FILE
FILE SECTION.
SECTION.
RecordK
RecordG
RecordG
PROCEDURE
PROCEDURE DIVISION.
DIVISION.
OPEN
INPUT
OPEN INPUT TF.
TF.
OPEN
INPUT
OF
OPEN INPUT OF
OPEN
OPEN OUTPUT
OUTPUT NF.
NF.
READ
TF.
READ TF.
READ
READ OF.
OF.
IF
TFKey
IF TFKey NOT
NOT == OFKey
OFKey
MOVE
OFRec
TO
MOVE OFRec TO NFRec
NFRec
WRITE
NFRec
WRITE NFRec
READ
READ OF
OF
ELSE
ELSE
READ
READ TF
TF
READ
OF
READ OF
END-IF.
END-IF.
New File
RecordA
RecordA
RecordG
RecordG
New File
RecordB
RecordB
RecordK
RecordK
RecordA
RecordA
RecordG
RecordG
RecordM
RecordM
Ordered File
RecordA
RecordA
RecordB
RecordB
RecordG
RecordG
RecordH
RecordH
RecordK
RecordK
RecordM
RecordM
RecordN
RecordN
RecordH
RecordH
RESULT
RecordN
RecordN
PROGRAM
FILE
FILE SECTION.
SECTION.
TFRec
OFRec
NFRec
PROCEDURE
PROCEDURE DIVISION.
DIVISION.
OPEN
INPUT
OPEN INPUT TF.
TF.
OPEN
INPUT
OF
OPEN INPUT OF
OPEN
OPEN OUTPUT
OUTPUT NF.
NF.
READ
TF.
READ TF.
READ
READ OF.
OF.
IF
TFKey
IF TFKey == OFKey
OFKey
Update
OFRec
Update OFRec with
with TFRec
TFRec
MOVE
OFRec+
TO
NFRec
MOVE OFRec+ TO NFRec
WRITE
WRITE NFRec
NFRec
READ
TF
READ TF
READ
READ OF
OF
ELSE
ELSE
MOVE
MOVE OFRec
OFRec TO
TO NFRec
NFRec
WRITE
NFRec
WRITE NFRec
READ
READ OF
OF
END-IF.
END-IF.
New File
PROGRAM
FILE
FILE SECTION.
SECTION.
RecordB
RecordA
RecordA
PROCEDURE
PROCEDURE DIVISION.
DIVISION.
OPEN
INPUT
OPEN INPUT TF.
TF.
OPEN
INPUT
OF
OPEN INPUT OF
OPEN
OPEN OUTPUT
OUTPUT NF.
NF.
READ
TF.
READ TF.
READ
READ OF.
OF.
IF
TFKey
IF TFKey == OFKey
OFKey
Update
OFRec
Update OFRec with
with TFRec
TFRec
MOVE
OFRec+
TO
NFRec
MOVE OFRec+ TO NFRec
WRITE
WRITE NFRec
NFRec
READ
TF
READ TF
READ
READ OF
OF
ELSE
ELSE
MOVE
MOVE OFRec
OFRec TO
TO NFRec
NFRec
WRITE
NFRec
WRITE NFRec
READ
READ OF
OF
END-IF.
END-IF.
New File
RecordA
RecordA
PROGRAM
FILE
FILE SECTION.
SECTION.
RecordB
RecordB
RecordB+
PROCEDURE
PROCEDURE DIVISION.
DIVISION.
OPEN
INPUT
OPEN INPUT TF.
TF.
OPEN
INPUT
OF
OPEN INPUT OF
OPEN
OPEN OUTPUT
OUTPUT NF.
NF.
READ
TF.
READ TF.
READ
READ OF.
OF.
IF
TFKey
IF TFKey == OFKey
OFKey
Update
OFRec
Update OFRec with
with TFRec
TFRec
MOVE
OFRec+
TO
NFRec
MOVE OFRec+ TO NFRec
WRITE
WRITE NFRec
NFRec
READ
TF
READ TF
READ
READ OF
OF
ELSE
ELSE
MOVE
MOVE OFRec
OFRec TO
TO NFRec
NFRec
WRITE
NFRec
WRITE NFRec
READ
READ OF
OF
END-IF.
END-IF.
New File
RecordA
RecordA
RecordB+
RecordB+
PROGRAM
FILE
FILE SECTION.
SECTION.
RecordH
RecordG
RecordG
PROCEDURE
PROCEDURE DIVISION.
DIVISION.
OPEN
INPUT
OPEN INPUT TF.
TF.
OPEN
INPUT
OF
OPEN INPUT OF
OPEN
OPEN OUTPUT
OUTPUT NF.
NF.
READ
TF.
READ TF.
READ
READ OF.
OF.
IF
TFKey
IF TFKey == OFKey
OFKey
Update
OFRec
Update OFRec with
with TFRec
TFRec
MOVE
OFRec+
TO
NFRec
MOVE OFRec+ TO NFRec
WRITE
WRITE NFRec
NFRec
READ
TF
READ TF
READ
READ OF
OF
ELSE
ELSE
MOVE
MOVE OFRec
OFRec TO
TO NFRec
NFRec
WRITE
NFRec
WRITE NFRec
READ
READ OF
OF
END-IF.
END-IF.
New File
RecordA
RecordA
RecordB+
RecordB+
RecordG
RecordG
PROGRAM
FILE
FILE SECTION.
SECTION.
TFRec
OFRec
NFRec
PROCEDURE
PROCEDURE DIVISION.
DIVISION.
OPEN
INPUT
OPEN INPUT TF.
TF.
OPEN
INPUT
OF
OPEN INPUT OF
OPEN
OPEN OUTPUT
OUTPUT NF.
NF.
READ
TF.
READ TF.
READ
READ OF.
OF.
IF
TFKey
IF TFKey << OFKey
OFKey
MOVE
TFRec
MOVE TFRec TO
TO NFRec
NFRec
WRITE
NFRec
WRITE NFRec
READ
READ TF
TF
ELSE
ELSE
MOVE
MOVE OFRec
OFRec TO
TO NFRec
NFRec
WRITE
NFRec
WRITE NFRec
READ
READ OF
OF
END-IF.
END-IF.
New File
PROGRAM
FILE
FILE SECTION.
SECTION.
RecordC
RecordA
RecordA
PROCEDURE
PROCEDURE DIVISION.
DIVISION.
OPEN
INPUT
OPEN INPUT TF.
TF.
OPEN
INPUT
OF
OPEN INPUT OF
OPEN
OPEN OUTPUT
OUTPUT NF.
NF.
READ
TF.
READ TF.
READ
READ OF.
OF.
IF
TFKey
IF TFKey << OFKey
OFKey
MOVE
TFRec
MOVE TFRec TO
TO NFRec
NFRec
WRITE
NFRec
WRITE NFRec
READ
READ TF
TF
ELSE
ELSE
MOVE
MOVE OFRec
OFRec TO
TO NFRec
NFRec
WRITE
NFRec
WRITE NFRec
READ
READ OF
OF
END-IF.
END-IF.
New File
RecordA
RecordA
PROGRAM
FILE
FILE SECTION.
SECTION.
RecordC
RecordB
RecordB
PROCEDURE
PROCEDURE DIVISION.
DIVISION.
OPEN
INPUT
OPEN INPUT TF.
TF.
OPEN
INPUT
OF
OPEN INPUT OF
OPEN
OPEN OUTPUT
OUTPUT NF.
NF.
READ
TF.
READ TF.
READ
READ OF.
OF.
IF
TFKey
IF TFKey << OFKey
OFKey
MOVE
TFRec
MOVE TFRec TO
TO NFRec
NFRec
WRITE
NFRec
WRITE NFRec
READ
READ TF
TF
ELSE
ELSE
MOVE
MOVE OFRec
OFRec TO
TO NFRec
NFRec
WRITE
NFRec
WRITE NFRec
READ
READ OF
OF
END-IF.
END-IF.
New File
RecordA
RecordA
RecordB
RecordB
PROGRAM
FILE
FILE SECTION.
SECTION.
RecordC
RecordG
RecordC
PROCEDURE
PROCEDURE DIVISION.
DIVISION.
OPEN
INPUT
OPEN INPUT TF.
TF.
OPEN
INPUT
OF
OPEN INPUT OF
OPEN
OPEN OUTPUT
OUTPUT NF.
NF.
READ
TF.
READ TF.
READ
READ OF.
OF.
IF
TFKey
IF TFKey << OFKey
OFKey
MOVE
TFRec
MOVE TFRec TO
TO NFRec
NFRec
WRITE
NFRec
WRITE NFRec
READ
READ TF
TF
ELSE
ELSE
MOVE
MOVE OFRec
OFRec TO
TO NFRec
NFRec
WRITE
NFRec
WRITE NFRec
READ
READ OF
OF
END-IF.
END-IF.
New File
RecordA
RecordA
RecordB
RecordB
RecordC
RecordC
PROGRAM
FILE
FILE SECTION.
SECTION.
RecordF
RecordG
RecordF
PROCEDURE
PROCEDURE DIVISION.
DIVISION.
OPEN
INPUT
OPEN INPUT TF.
TF.
OPEN
INPUT
OF
OPEN INPUT OF
OPEN
OPEN OUTPUT
OUTPUT NF.
NF.
READ
TF.
READ TF.
READ
READ OF.
OF.
IF
TFKey
IF TFKey << OFKey
OFKey
MOVE
TFRec
MOVE TFRec TO
TO NFRec
NFRec
WRITE
NFRec
WRITE NFRec
READ
READ TF
TF
ELSE
ELSE
MOVE
MOVE OFRec
OFRec TO
TO NFRec
NFRec
WRITE
NFRec
WRITE NFRec
READ
READ OF
OF
END-IF.
END-IF.
New File
RecordA
RecordA
RecordB
RecordB
RecordC
RecordC
RecordF
RecordF
PROGRAM
FILE
FILE SECTION.
SECTION.
RecordP
RecordG
RecordG
PROCEDURE
PROCEDURE DIVISION.
DIVISION.
OPEN
INPUT
OPEN INPUT TF.
TF.
OPEN
INPUT
OF
OPEN INPUT OF
OPEN
OPEN OUTPUT
OUTPUT NF.
NF.
READ
TF.
READ TF.
READ
READ OF.
OF.
IF
TFKey
IF TFKey << OFKey
OFKey
MOVE
TFRec
MOVE TFRec TO
TO NFRec
NFRec
WRITE
NFRec
WRITE NFRec
READ
READ TF
TF
ELSE
ELSE
MOVE
MOVE OFRec
OFRec TO
TO NFRec
NFRec
WRITE
NFRec
WRITE NFRec
READ
READ OF
OF
END-IF.
END-IF.
New File
RecordA
RecordA
RecordB
RecordB
RecordC
RecordC
RecordF
RecordF
RecordG
RecordG
Advanced
Sequential
Files 1.
For example
PIC 9(7).
PIC X(8).
PIC XX.
PIC
PIC
PIC
PIC
PIC
PIC
What is not
obvious from this
description is that
COBOL continues to
create just a single
record buffer for
the file!
9(2).
9(2).
9(2).
X(4).
9(4).
X.
01 DeleteRec.
02 StudentId
PIC 9(7).
01 UpdateRec.
02 StudentId
02 OldCourseCode
02 NewCourseCode
PIC 9(7).
PIC X(4).
PIC X(4).
TransactionFile Buffer
9 2 3 0 1 6 5 H E N N E S SYR M 7 1 0 9 1 5 LM 5 1 0 5 5 0 F
TransactionFile Buffer
InsertionRec
StudentId StudentName
DateOfBirth CourseCode
Grant Gender
9 2 3 0 1 6 5 H E N N E S SYR M 7 1 0 9 1 5 LM 5 1 0 5 5 0 F
TransactionFile Buffer
InsertionRec
StudentId StudentName
DateOfBirth CourseCode
Grant Gender
9 2 3 0 1 6 5 H E N N E S SYR M 7 1 0 9 1 5 LM 5 1 0 5 5 0 F
StudentId
DeletionRec
TransactionFile Buffer
InsertionRec
StudentId StudentName
DateOfBirth CourseCode
Grant Gender
9 2 3 0 1 6 5 H E N N E S SYR M 7 1 0 9 1 5 LM 5 1 0 5 5 0 F
StudentId
DeletionRec
StudentId
OldCourseCode NewCourseCode
UpdateRec
TransactionFile Buffer
InsertionRec
StudentId StudentName
DateOfBirth CourseCode
Grant Gender
9 2 3 0 1 6 5 H E N N E S SYR M 7 1 0 9 1 5 LM 5 1 0 5 5 0 F
StudentId
DeletionRec
StudentId
OldCourseCode NewCourseCode
UpdateRec
DATA DIVISION.
FILE SECTION.
FD TransactionFile.
01 InsertionRec.
02 TransCode
02 StudentId
02 StudentName.
03 Surname
03 Initials
02 DateOfBirth.
03 YOBirth
03 MOBirth
03 DOBirth
02 CourseCode
02 Grant
02 Gender
PIC X.
PIC 9(7).
PIC X(8).
PIC XX.
PIC
PIC
PIC
PIC
PIC
PIC
9(2).
9(2).
9(2).
X(4).
9(4).
X.
01 DeleteRec.
02 TransCode
02 StudentId
PIC X.
PIC 9(7).
01 UpdateRec.
02 TransCode
02 StudentId
02 OldCourseCode
02 NewCourseCode
PIC
PIC
PIC
PIC
X.
9(7).
X(4).
X(4).
VALUE HIGH-VALUES.
PIC X.
VALUE "I".
VALUE "D".
VALUE "U".
PIC 9(7).
TransCode and
StudentId have the
same description
and are in the same
location in all three
records.
PIC X(8).
PIC XX.
PIC
PIC
PIC
PIC
PIC
PIC
9(2).
9(2).
9(2).
X(4).
9(4).
X.
01 DeleteRec.
02 FILLER
PIC X(8).
01 UpdateRec.
02 FILLER
02 OldCourseCode
02 NewCourseCode
PIC X(8).
PIC X(4).
PIC X(4).
TransactionFile Buffer
InsertionRec
TransCode StudentId StudentName DateOfBirth CourseCode Grant Gender
I 9 2 3 0 1 6 5 H E N N E S SYR M 7 1 0 9 1 5 LM 5 1 0 5 5 0 F
FILLER
DeletionRec
FILLER
OldCourseCode NewCourseCode
UpdateRec
TransactionFile Buffer
InsertionRec
TransCode StudentId StudentName DateOfBirth CourseCode Grant Gender
U 9 3 1 5 6 8 2 LM 6 1 LM 5 1 R M 7 1 0 9 1 5 LM 5 1 0 5 5 0 F
FILLER
DeletionRec
FILLER
OldCourseCode NewCourseCode
UpdateRec
Printing a Report.
Page Heading.
Page Footing.
Page : PageNum
Column Headings.
Report Footing.
PageHeading.
02 FILLER
PIC X(7) VALUE SPACES.
02 FILLER
PIC X(25)
VALUE "UL Student Details Report".
01
PageFooting.
02 FILLER
02 FILLER
02 FILLER
01
01
StudentDetailLine.
02 PrnStudId
PIC
02 PrnStudName PIC
02 PrnGender
PIC
02 PrnCourse
PIC
01
ReportFooting
PIC X(38)
VALUE "*** End of Student Details Report ***".
BB9(7).
BBX(10).
BBBBX.
BBBBX(4).
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT Printer
ASSIGN TO LPT1.
DATA DIVISION.
FILE SECTION.
FD Printer.
01 PrintLine.
????????????????
In previous
lectures we saw
that the file buffer
is represented by a record description (01 level).
But !!
These record types all map on to the same area of storage and
print line records cannot share the same area of storage.
Why? Because most of the print line record values are
assigned using the VALUE clause and these values are
assigned as soon as the program starts.
To prevent us trying to use the VALUE clause to assign values
to a File buffer COBOL has a rule which states that;
In the FILE SECTION, the VALUE clause must be used
in
condition-name entries only (i.e. it cannot be used
to give an
initial value to an item).
A Solution
We get round the problem as follows;
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT ReportFile ASSIGN TO STUDENTS.RPT.
DATA DIVISION.
FILE SECTION.
FD ReportFile.
01 PrintLine
PIC X(38).
DISK
WORKING-STORAGE SECTION.
01 PageHeading.
STUDENTS.RPT
02 FILLER
PIC X(7) VALUE SPACES.
02 FILLER
PIC X(25)
VALUE "UL Student Details Report".
01 PageFooting.
02 FILLER
PIC X(19) VALUE SPACES.
02 FILLER
PIC X(7) VALUE "Page : ".
02 FILLER
PIC 99.
01 ColumnHeadings PIC X(36)
VALUE " StudentId StudentName Gender Course".
01 StudentDetailLine.
02 PrnStudId
PIC BB9(7).
02 PrnStudName PIC BBX(10).
02 PrnGender
PIC BBBBX.
02 PrnCourse
PIC BBBBX(4).
01 ReportFooting
PIC X(38)
VALUE "*** End of Student Details Report ***".
BEFORE
AFTER
LINE
AdvanceNum
LINES
ADVANCING MnemonicName
PAGE
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT ReportFile ASSIGN TO "STUDENTS.RPT"
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD ReportFile.
01 PrintLine
PIC X(40).
WORKING-STORAGE SECTION.
01 HeadingLine
PIC X(21) VALUE " Record Count
01 StudentTotalLine.
02 FILLER
PIC X(17) VALUE "Total Students
02 PrnStudentCount PIC Z,ZZ9.
01 MaleTotalLine.
02 FILLER
PIC X(17) VALUE "Total Males
02 PrnMaleCount
PIC Z,ZZ9.
01 FemaleTotalLine.
02 FILLER
PIC X(17) VALUE "Total Females
02 PrnFemaleCount PIC Z,ZZ9.
MOVE StudentCount TO
MOVE MaleCount
TO
MOVE FemaleCount TO
WRITE PrintLine FROM
WRITE PrintLine FROM
WRITE PrintLine FROM
WRITE PrintLine FROM
PrnStudentCount
PrnMaleCount
PrnFemaleCount
HeadingLine
StudentTotalLine
MaleTotalLine
FemaleTotalLine
AFTER
AFTER
AFTER
AFTER
DISK
STUDENTS.RPT
Report".
= ".
= ".
= ".
ADVANCING
ADVANCING
ADVANCING
ADVANCING
PAGE
2 LINES
2 LINES
2 LINES.
Introduction to
Direct Access
Files.
Rec001
Rec005
Rec045
Rec090
Rec100
Rec150
Rec300
Rec325
^Z
Rec085
Unordered
Rec300
Rec150
Rec005
Rec090
Rec045
Rec100
Rec001
Rec325
^Z
Rec001
Rec005
Rec045
Rec085
Rec090
Rec100
Rec150
Rec300
Rec325
^Z
Rec085
Extend-Unordered
Rec300
Rec150
Rec005
Rec090
Rec045
Rec100
Rec001
Rec325
Rec085
^Z
Rec001
Rec005
Rec045
Rec090
Rec100
Rec150
Rec300
Rec325
^Z
Rec150
Unordered
Rec300
Rec150
Rec005
Rec090
Rec045
Rec100
Rec001
Rec325
^Z
Rec001
Rec005
Rec045
Rec090
Rec100
Rec300
Rec325
^Z
Rec150
New-Unordered
Rec300
Rec005
Rec090
Rec045
Rec100
Rec001
Rec325
^Z
Rec001
Rec005
Rec045
Rec090
Rec100
Rec150
Rec300
Rec325
^Z
Rec045
Unordered
Rec300
Rec150
Rec005
Rec090
Rec045
Rec100
Rec001
Rec325
^Z
Rec001
Rec005
Rec045
Rec090
Rec100
Rec150
Rec300
Rec325
^Z
Rec045
New-Unordered
Rec300
Rec150
Rec005
Rec090
Rec045
Rec100
Rec001
Rec325
^Z
4
5
6
7
325
326
327
328
Rec001
free
Rec003
Rec004
free
free
Rec007
Rec325
Rec326
free
Rec328
4
5
6
7
325
326
327
328
Rec001
free
Rec003
Rec004
free
free
Rec007
Rec325
Rec326
free
Rec328
Rec327
4
5
6
7
325
326
327
328
Rec001
free
Rec003
Rec004
free
free
Rec007
Rec325
Rec326
Rec327
Rec328
Rec327
4
5
6
7
325
326
327
328
Rec001
free
Rec003
Rec004
free
free
Rec007
Rec325
Rec326
free
Rec328
Rec325
4
5
6
7
325
326
327
328
Rec001
free
Rec003
Rec004
free
free
Rec007
deleted/free
Rec326
free
Rec328
Rec325
4
5
6
7
325
326
327
328
Rec001
free
Rec003
Rec004
free
free
Rec007
Rec325
Rec326
free
Rec328
Rec007
4
5
6
7
325
326
327
328
Rec001
free
Rec003
Rec004
free
free
Rec007
Rec325
Rec326
free
Rec328
Rec007
C F H
L O R
Mi Nf Ni Nt Oi Ot
Data Records
T W Z
C F H
L O R
Mi Nf Ni Nt Oi Ot
Data Records
T W Z
C F H
L O R
Mi Nf Ni Nt Oi Ot
Data Records
T W Z
C F H
L O R
Mi Nf Ni Nt Oi Ot
Data Records
T W Z
Sequential Files.
Disadvantages.
Advantages.
Simple organization.
Relative Files.
Disadvantages.
Advantages.
Indexed Files.
Disadvantages.
Advantages.
Relative
Files.
RUN
RUN OF
OF REL-EG2.EXE
REL-EG2.EXE USING
USING SEQUENTIAL
SEQUENTIAL READING
READING
Enter
Read
type
(Direct=1,
Seq=2)->
2
Enter Read type (Direct=1, Seq=2)-> 2
01
OVER
01 VESTRON
VESTRON VIDEOS
VIDEOS
OVER THE
THE SEA
SEA SOMEWHERE
SOMEWHERE IN
IN LONDON
LONDON
02
HOLLYWOOD,
02 EMI
EMI STUDIOS
STUDIOS
HOLLYWOOD, CALIFORNIA,
CALIFORNIA, USA
USA
03
BUSH
03 BBC
BBC WILDLIFE
WILDLIFE
BUSH HOUSE,
HOUSE, LONDON,
LONDON, ENGLAND
ENGLAND
04
HOLLYWOOD,
04 CBS
CBS STUDIOS
STUDIOS
HOLLYWOOD, CALIFORNIA,
CALIFORNIA, USA
USA
05
YACHTING
MONTHLY
TREE
HOUSE,
LONDON,
ENGLAND
05 YACHTING MONTHLY
TREE HOUSE, LONDON, ENGLAND
06
VIRGIN
VIDEOS
IS
06 VIRGIN VIDEOS
IS THIS
THIS ONE
ONE ALSO
ALSO LOCATED
LOCATED IN
IN ENGLAND
ENGLAND
07
NEW
07 CIC
CIC VIDEOS
VIDEOS
NEW YORK
YORK PLAZZA,
PLAZZA, NEW
NEW YORK,
YORK, USA
USA
RUN
RUN OF
OF REL-EG2.EXE
REL-EG2.EXE USING
USING DIRECT
DIRECT READ
READ
Enter
Enter Read
Read type
type (Direct=1,
(Direct=1, Seq=2)->
Seq=2)-> 11
Enter
Enter supplier
supplier key
key (2
(2 digits)->
digits)-> 05
05
05
TREE
05 YACHTING
YACHTING MONTHLY
MONTHLY
TREE HOUSE,
HOUSE, LONDON,
LONDON, ENGLAND
ENGLAND
Indexed
Files.
04
04
RUN
OF
INDEX-EG3.EXE
USING
VIDEOCODE
RUN
OF
INDEX-EG3.EXE
USING
VIDEOCODE = 2 -> 1
Chose
key
VideoCode
=
1,
VideoTitle
Chose Video
key VideoCode
= 1, VideoTitle
= 2 -> 1
Enter
Code
(5
digits)
->
05051
Enter
Video
Code
(5
digits)
->
05051
05051
OVERBOARD
05051
OVERBOARD
01
01
RUN
INDEX-EG3.EXE
USING
VIDEOTITLE
RUN OF
OF
INDEX-EG3.EXE
USING
VIDEOTITLE= 2 -> 2
Chose
key
VideoCode
=
1,
VideoTitle
Chose
key
VideoCode
=
1,
VideoTitle
= 2 -> 2
Enter
Video
Title
(40
chars)
->
OVERBOARD
Enter Video
Title (40 chars) -> OVERBOARD
05051
OVERBOARD
05051
OVERBOARD
01
01
RUN
INDEX-EG3.EXE
USING
VIDEOTITLE
RUN OF
OF
INDEX-EG3.EXE
USING
VIDEOTITLE= 2 -> 2
Chose
key
VideoCode
=
1,
VideoTitle
Chose
key
VideoCode
=
1,
VideoTitle
=DANCING
2 -> 2
Enter
Video
Title
(40
chars)
->
DIRTY
Enter
Video
Title
(40
chars)
->
DIRTY
DANCING
02121
DIRTY
02121
DIRTY DANCING
DANCING
04
04
RUN
OF
INDEX-EG3.EXE
USING
NON
EXISTANT
VIDEOCODE
RUN
OF
INDEX-EG3.EXE
USING
NON
EXISTANT
VIDEOCODE
Chose
key
VideoCode
=
1,
VideoTitle
=
2
Chose Video
key VideoCode
= 1, VideoTitle
= 2 ->
-> 11
Enter
Code
(5
digits)
->
44444
Enter STATUS
Video Code
(5 digits) -> 44444
VIDEO
:23
VIDEO STATUS :- 23
30 60 99
10 20 30
40 50 60
Level 2
70 80 99
Level 1
Level 0
41 43 44 45 46 49
Data Buckets
C F H
H R Z
L O R
Level 2
T W Z
Level 1
Level 0
Mi Nf Ni Nt Oi Ot
Base Buckets
50 51 54 55 56 59
Ii Ef Bi Nt Jt At
Data Buckets
H R Z
C F H
L O R
Level 2
T W Z
Level 1
Level 0
Mi Nf Ni Nt Oi Ot
Base Buckets
Ot
45
Data Buckets
50 51 54 55 56 59
Ii Ef Bi Nt Jt At
Nf
65
Mi
71
STRING
STRING Syntax.
STRING Example 1
01 DayStr
PIC XX.
J U N E
01 YearStr
PIC X(4).
1 9 9 4
01 DateStr
- - - - - - STRING DayStr
", "
MonthStr
", "
YearStr
INTO DateStr
END-STRING.
- - - - - - - -
DELIMITED
DELIMITED
DELIMITED
DELIMITED
DELIMITED
BY
BY
BY
BY
BY
SPACES
SIZE
SPACES
SIZE
SIZE
STRING Example 1
01 DayStr
PIC XX.
J U N E
01 YearStr
PIC X(4).
1 9 9 4
01 DateStr
5 - - - - - STRING DayStr
", "
MonthStr
", "
YearStr
INTO DateStr
END-STRING.
- - - - - - - -
DELIMITED
DELIMITED
DELIMITED
DELIMITED
DELIMITED
BY
BY
BY
BY
BY
SPACES
SIZE
SPACES
SIZE
SIZE
STRING Example 1
01 DayStr
PIC XX.
J U N E
01 YearStr
PIC X(4).
1 9 9 4
01 DateStr
5 , - - - - STRING DayStr
", "
MonthStr
", "
YearStr
INTO DateStr
END-STRING.
- - - - - - - -
DELIMITED
DELIMITED
DELIMITED
DELIMITED
DELIMITED
BY
BY
BY
BY
BY
SPACES
SIZE
SPACES
SIZE
SIZE
STRING Example 1
01 DayStr
PIC XX.
J U N E
01 YearStr
PIC X(4).
1 9 9 4
01 DateStr
5 , J U N E STRING DayStr
", "
MonthStr
", "
YearStr
INTO DateStr
END-STRING.
- - - - - - - -
DELIMITED
DELIMITED
DELIMITED
DELIMITED
DELIMITED
BY
BY
BY
BY
BY
SPACES
SIZE
SPACES
SIZE
SIZE
STRING Example 1
01 DayStr
PIC XX.
J U N E
01 YearStr
PIC X(4).
1 9 9 4
01 DateStr
5 , J U N E ,
STRING DayStr
", "
MonthStr
", "
YearStr
INTO DateStr
END-STRING.
- - - - - - - -
DELIMITED
DELIMITED
DELIMITED
DELIMITED
DELIMITED
BY
BY
BY
BY
BY
SPACES
SIZE
SPACES
SIZE
SIZE
STRING Example 1
01 DayStr
PIC XX.
J U N E
01 YearStr
PIC X(4).
1 9 9 4
01 DateStr
5 , J U N E , 1 9 9 4 - - - STRING DayStr
", "
MonthStr
", "
YearStr
INTO DateStr
END-STRING.
DELIMITED
DELIMITED
DELIMITED
DELIMITED
DELIMITED
BY
BY
BY
BY
BY
SPACES
SIZE
SPACES
SIZE
SIZE
STRING Example 2
01 StrPtr
PIC 99.
01 DayStr
PIC XX.
5
J U N E
01 YearStr
PIC X(4).
1 9 9 4
01 DateStr
- - - - - - - -
- - - - - - -
MOVE 1 TO StrPtr
STRING DayStr
DELIMITED BY SPACES
","
DELIMITED BY SIZE
INTO DateStr WITH POINTER StrPtr
END-STRING.
STRING MonthStr DELIMITED BY SPACES
","
DELIMITED BY SIZE
INTO DateStr WITH POINTER StrPtr
END-STRING.
STRING YearStr DELIMITED BY SIZE
INTO DateStr WITH POINTER StrPtr
END-STRING.
STRING Example 2
01 StrPtr
PIC 99.
01 DayStr
PIC XX.
5
J U N E
01 YearStr
PIC X(4).
1 9 9 4
01 DateStr
5 , - - - - - - - - - - - - MOVE 1 TO StrPtr
STRING DayStr
DELIMITED BY SPACES
","
DELIMITED BY SIZE
INTO DateStr WITH POINTER StrPtr
END-STRING.
STRING MonthStr DELIMITED BY SPACES
","
DELIMITED BY SIZE
INTO DateStr WITH POINTER StrPtr
END-STRING.
STRING YearStr DELIMITED BY SIZE
INTO DateStr WITH POINTER StrPtr
END-STRING.
STRING Example 2
01 StrPtr
PIC 99.
01 DayStr
PIC XX.
5
J U N E
01 YearStr
PIC X(4).
1 9 9 4
01 DateStr
5 , J U N E , - - - - - - - MOVE 1 TO StrPtr
STRING DayStr
DELIMITED BY SPACES
","
DELIMITED BY SIZE
INTO DateStr WITH POINTER StrPtr
END-STRING.
STRING MonthStr DELIMITED BY SPACES
","
DELIMITED BY SIZE
INTO DateStr WITH POINTER StrPtr
END-STRING.
STRING YearStr DELIMITED BY SIZE
INTO DateStr WITH POINTER StrPtr
END-STRING.
STRING Example 2
01 StrPtr
PIC 99.
01 DayStr
PIC XX.
5
J U N E
01 YearStr
PIC X(4).
1 9 9 4
01 DateStr
5 , J U N E , 1 9 9 4 - - - MOVE 1 TO StrPtr
STRING DayStr
DELIMITED BY SPACES
","
DELIMITED BY SIZE
INTO DateStr WITH POINTER StrPtr
END-STRING.
STRING MonthStr DELIMITED BY SPACES
","
DELIMITED BY SIZE
INTO DateStr WITH POINTER StrPtr
END-STRING.
STRING YearStr DELIMITED BY SIZE
INTO DateStr WITH POINTER StrPtr
END-STRING.
STRING Example 3
01 StringFields.
02 Field1 PIC X(18) VALUE "Where does this go".
02 Field2 PIC X(30)
VALUE "This is the destination string".
02 Field3 PIC X(15) VALUE "Here is another".
01 StrPointers.
02 StrPtr PIC 99.
02 NewPtr PIC 9.
STRING Example 3
01 StringFields.
02 Field1 PIC X(18) VALUE "Where does this go".
02 Field2 PIC X(30)
VALUE "This is the destination string".
02 Field3 PIC X(15) VALUE "Here is another".
01 StrPointers.
02 StrPtr PIC 99.
02 NewPtr PIC 9.
STRING Example 4
01 StringFields.
02 Field1 PIC X(18) VALUE "Where does this go".
02 Field2 PIC X(30)
VALUE "This is the destination string".
02 Field3 PIC X(15) VALUE "Here is another".
01 StrPointers.
02 StrPtr PIC 99.
02 NewPtr PIC 9.
STRING Example 4
01 StringFields.
02 Field1 PIC X(18) VALUE "Where does this go".
02 Field2 PIC X(30)
VALUE "This is the destination string".
02 Field3 PIC X(15) VALUE "Here is another".
01 StrPointers.
02 StrPtr PIC 99.
02 NewPtr PIC 9.
STRING Example 5
01 StringFields.
02 Field1 PIC X(18) VALUE "Where does this go".
02 Field2 PIC X(30)
VALUE "This is the destination string".
02 Field3 PIC X(15) VALUE "Here is another".
01 StrPointers.
02 StrPtr PIC 99.
02 NewPtr PIC 9.
STRING Example 5
01 StringFields.
02 Field1 PIC X(18) VALUE "Where does this go".
02 Field2 PIC X(30)
VALUE "This is the destination string".
02 Field3 PIC X(15) VALUE "Here is another".
01 StrPointers.
02 StrPtr PIC 99.
02 NewPtr PIC 9.
STRING Example 6
01 StringFields.
02 Field1 PIC X(18) VALUE "Where does this go".
02 Field2 PIC X(30)
VALUE "This is the destination string".
02 Field3 PIC X(15) VALUE "Here is another".
01 StrPointers.
02 StrPtr PIC 99.
02 NewPtr PIC 9.
STRING Example 6
01 StringFields.
02 Field1 PIC X(18) VALUE "Where does this go".
02 Field2 PIC X(30)
VALUE "This is the destination string".
02 Field3 PIC X(15) VALUE "Here is another".
01 StrPointers.
02 StrPtr PIC 99.
02 NewPtr PIC 9.
WhereThisHere
STRING Example 7
01 StringFields.
02 Field1 PIC X(18) VALUE "Where does this go".
02 Field2 PIC X(30)
VALUE "This is the destination string".
02 Field3 PIC X(15) VALUE "Here is another".
01 StrPointers.
02 StrPtr PIC 99.
02 NewPtr PIC 9.
STRING Example 7
01 StringFields.
02 Field1 PIC X(18) VALUE "Where does this go".
02 Field2 PIC X(30)
VALUE "This is the destination string".
02 Field3 PIC X(15) VALUE "Here is another".
01 StrPointers.
02 StrPtr PIC 99.
02 NewPtr PIC 9.
STRING Example 8
01 StringFields.
02 Field1 PIC X(18) VALUE "Where does this go".
02 Field2 PIC X(30)
VALUE "This is the destination string".
02 Field3 PIC X(15) VALUE "Here is another".
01 StrPointers.
02 StrPtr PIC 99.
02 NewPtr PIC 9.
STRING Example 8
01 StringFields.
02 Field1 PIC X(18) VALUE "Where does this go".
02 Field2 PIC X(30)
VALUE "This is the destination string".
02 Field3 PIC X(15) VALUE "Here is another".
01 StrPointers.
02 StrPtr PIC 99.
02 NewPtr PIC 9.
UNSTRING
UNSTRING Syntax.
UNSTRING Termination
The UNSTRING statement terminates when:All the characters in the Source String have been examined
OR
All the Destination Strings have been processed
OR
Some error condition is encountered.
UNSTRING clauses.
ON OVERFLOW.
The ON OVERFLOW is activated if : The Unstring pointer (Pointer#i) is not pointing to a
character position within the SourceString when the
UNSTRING executes.
All the Destination Strings have been processed but there
are still valid unexamined characters in the Source
String.
COUNT IN
The COUNT IN clause is associated with a particular
Destination String and holds a count of the number of
characters passed to the Destination String.
TALLYING IN
Only one TALLYING clause can be used with each
UNSTRING. It holds a count of the number of
Destination Strings affected by the UNSTRING operation.
WITH POINTER
The Pointer#i holds the position of the next nondelimiter character to be examined in the Source
String.
Pointer#i must be large enough to hold a value one
greater than the size of the Source String.
DELIMITER IN
A DELIMITER IN clause is associated with a particular
Destination String. HoldDelim$i holds the Delimiter
that was encountered in the Source String.
ALL
When the ALL phrase is used, contiguous
delimiters are treated as if only one delimiter had
been encountered.
UNSTRING Example 1
01 DayStr
PIC XX.
PIC XX.
01 DateStr
PIC X(8).
1 9 - 0 5 - 8 0
ACCEPT DateStr.
UNSTRING DateStr
INTO DayStr, MonthStr, YearStr
ON OVERFLOW DISPLAY "Chars Left"
END-UNSTRING.
UNSTRING Example 1
01 DayStr
PIC XX.
1 9
PIC XX.
01 DateStr
PIC X(8).
1 9 - 0 5 - 8 0
ACCEPT DateStr.
UNSTRING DateStr
INTO DayStr, MonthStr, YearStr
ON OVERFLOW DISPLAY "Chars Left"
END-UNSTRING.
UNSTRING Example 1
PIC XX.
1 9
- 0
01 DayStr
01 YearStr
PIC XX.
01 DateStr
PIC X(8).
1 9 - 0 5 - 8 0
ACCEPT DateStr.
UNSTRING DateStr
INTO DayStr, MonthStr, YearStr
ON OVERFLOW DISPLAY "Chars Left"
END-UNSTRING.
UNSTRING Example 1
PIC XX.
1 9
- 0
01 YearStr
PIC XX.
5 -
01 DateStr
PIC X(8).
1 9 - 0 5 - 8 0
01 DayStr
ACCEPT DateStr.
UNSTRING DateStr
INTO DayStr, MonthStr, YearStr
ON OVERFLOW DISPLAY "Chars Left"
END-UNSTRING.
UNSTRING Example 1
PIC XX.
1 9
- 0
01 YearStr
PIC XX.
5 -
01 DateStr
PIC X(8).
1 9 - 0 5 - 8 0
01 DayStr
ACCEPT DateStr.
UNSTRING DateStr
INTO DayStr, MonthStr, YearStr
ON OVERFLOW DISPLAY "Chars Left"
END-UNSTRING.
Chars
Chars Left
Left
UNSTRING Example 2
01 DayStr
PIC XX.
PIC XX.
01 DateStr
PIC X(8).
1 9 s t o p 0 5 s t o p 8 0
ACCEPT DateStr.
UNSTRING DateStr DELIMITED BY "stop"
INTO DayStr, MonthStr, YearStr
ON OVERFLOW DISPLAY "Chars Left"
END-UNSTRING.
UNSTRING Example 2
01 DayStr
PIC XX.
1 9
PIC XX.
01 DateStr
PIC X(8).
1 9 s t o p 0 5 s t o p 8 0
ACCEPT DateStr.
UNSTRING DateStr DELIMITED BY "stop"
INTO DayStr, MonthStr, YearStr
ON OVERFLOW DISPLAY "Chars Left"
END-UNSTRING.
UNSTRING Example 2
PIC XX.
1 9
0 5
01 DayStr
01 YearStr
PIC XX.
01 DateStr
PIC X(8).
1 9 s t o p 0 5 s t o p 8 0
ACCEPT DateStr.
UNSTRING DateStr DELIMITED BY "stop"
INTO DayStr, MonthStr, YearStr
ON OVERFLOW DISPLAY "Chars Left"
END-UNSTRING.
UNSTRING Example 2
PIC XX.
1 9
0 5
01 YearStr
PIC XX.
8 0
01 DateStr
PIC X(8).
01 DayStr
1 9 s t o p 0 5 s t o p 8 0
ACCEPT DateStr.
UNSTRING DateStr DELIMITED BY "stop"
INTO DayStr, MonthStr, YearStr
ON OVERFLOW DISPLAY "Chars Left"
END-UNSTRING.
UNSTRING Example 3
01 DayStr
PIC XX.
1 9
PIC XX.
01 DateStr
PIC X(8).
1 9 - 0 5 / 8 0
ACCEPT DateStr.
UNSTRING DateStr
DELIMITED BY "/" OR "-"
INTO DayStr
DELIMITER IN Hold1
MonthStr DELIMITER IN Hold2
YearStr
END-UNSTRING.
DISPLAY DayStr SPACE MonthStr SPACE YearStr.
DISPLAY Hold1 SPACE Hold2
UNSTRING Example 3
PIC XX.
1 9
0 5
01 DayStr
01 YearStr
PIC XX.
01 DateStr
PIC X(8).
1 9 - 0 5 / 8 0
ACCEPT DateStr.
UNSTRING DateStr
DELIMITED BY "/" OR "-"
INTO DayStr
DELIMITER IN Hold1
MonthStr DELIMITER IN Hold2
YearStr
END-UNSTRING.
DISPLAY DayStr SPACE MonthStr SPACE YearStr.
DISPLAY Hold1 SPACE Hold2
UNSTRING Example 3
PIC XX.
1 9
0 5
01 YearStr
PIC XX.
8 0
01 DateStr
PIC X(8).
1 9 - 0 5 / 8 0
01 DayStr
ACCEPT DateStr.
UNSTRING DateStr
DELIMITED BY "/" OR "-"
INTO DayStr
DELIMITER IN Hold1
MonthStr DELIMITER IN Hold2
YearStr
END-UNSTRING.
DISPLAY DayStr SPACE MonthStr SPACE YearStr.
DISPLAY Hold1 SPACE Hold2
UNSTRING Example 3
PIC XX.
1 9
0 5
01 YearStr
PIC XX.
8 0
01 DateStr
PIC X(8).
1 9 - 0 5 / 8 0
01 DayStr
ACCEPT DateStr.
UNSTRING DateStr
DELIMITED BY "/" OR "-"
INTO DayStr
DELIMITER IN Hold1
MonthStr DELIMITER IN Hold2 19
19 05
05
YearStr
-- //
END-UNSTRING.
DISPLAY DayStr SPACE MonthStr SPACE YearStr.
DISPLAY Hold1 SPACE Hold2
80
80
UNSTRING Example 4
01 DayStr
PIC XX.
PIC XX.
01 DateStr
PIC X(8).
1 9 - 0 5 / 8 0
ACCEPT DateStr.
UNSTRING DateStr
DELIMITED BY "/" OR "-"
INTO DayStr
DELIMITER IN Hold1
MonthStr DELIMITER IN Hold1
YearStr
END-UNSTRING.
DISPLAY DayStr SPACE MonthStr SPACE YearStr.
DISPLAY Hold1
UNSTRING Example 4
PIC XX.
1 9
0 5
01 YearStr
PIC XX.
8 0
01 DateStr
PIC X(8).
1 9 - 0 5 / 8 0
01 DayStr
ACCEPT DateStr.
UNSTRING DateStr
DELIMITED BY "/" OR "-"
INTO DayStr
DELIMITER IN Hold1
MonthStr DELIMITER IN Hold1 19
19 05
05
YearStr
//
END-UNSTRING.
DISPLAY DayStr SPACE MonthStr SPACE YearStr.
DISPLAY Hold1
80
80
UNSTRING Example 5
01 DayStr
PIC XX.
PIC XX.
01 DateStr
PIC X(11).
1 5 - - - 0 7 - - 9 4
ACCEPT DateStr.
UNSTRING DateStr
DELIMITED BY ALL "-"
INTO DayStr, MonthStr, YearStr
ON OVERFLOW DISPLAY "Chars Left"
END-UNSTRING.
UNSTRING Example 5
PIC XX.
1 5
0 7
01 YearStr
PIC XX.
9 4
01 DateStr
PIC X(11).
1 5 - - - 0 7 - - 9 4
01 DayStr
ACCEPT DateStr.
UNSTRING DateStr
DELIMITED BY ALL "-"
INTO DayStr, MonthStr, YearStr
ON OVERFLOW DISPLAY "Chars Left"
END-UNSTRING.
UNSTRING Example 6
01 DayStr
PIC XX.
PIC XX.
01 DateStr
PIC X(11).
1 5 - - - 0 7 - - 9 4
ACCEPT DateStr.
UNSTRING DateStr
DELIMITED BY "-"
INTO DayStr, MonthStr, YearStr
ON OVERFLOW DISPLAY "Chars Left"
END-UNSTRING.
UNSTRING Example 6
01 DayStr
PIC XX.
1 5
PIC XX.
01 DateStr
PIC X(11).
1 5 - - - 0 7 - - 9 4
ACCEPT DateStr.
UNSTRING DateStr
DELIMITED BY "-"
INTO DayStr, MonthStr, YearStr
ON OVERFLOW DISPLAY "Chars Left"
END-UNSTRING.
Chars
Chars Left
Left
UNSTRING Example 7
01 OldName
Tim
PIC X(80).
John
Roberts
01 TempName.
02 NameInitial PIC
02 FILLER
PIC
01 NewName
PIC
01 Pointers.
02 StrPtr
PIC
02 UnstrPtr
PIC
88 NameProcessed
X.
X(15).
X(30).
99 VALUE 1.
99 VALUE 1.
VALUE 81.
PROCEDURE DIVISION.
ProcessName.
ACCEPT OldName.
PERFORM UNTIL NameProcessed
UNSTRING OldName DELIMITED BY ALL SPACES
INTO TempName WITH POINTER UnstrPtr
END-UNSTRING
Display TempName
END-PERFORM
STOP RUN.
UNSTRING Example 7
01 OldName
Tim
PIC X(80).
John
Roberts
01 TempName.
02 NameInitial PIC
02 FILLER
PIC
01 NewName
PIC
01 Pointers.
02 StrPtr
PIC
02 UnstrPtr
PIC
88 NameProcessed
T im
X.
X(15).
X(30).
99 VALUE 1.
99 VALUE 1.
VALUE 81.
Tim
Tim
PROCEDURE DIVISION.
ProcessName.
ACCEPT OldName.
PERFORM UNTIL NameProcessed
UNSTRING OldName DELIMITED BY ALL SPACES
INTO TempName WITH POINTER UnstrPtr
END-UNSTRING
Display TempName
END-PERFORM
STOP RUN.
UNSTRING Example 7
01 OldName
Tim
PIC X(80).
John
Roberts
01 TempName.
02 NameInitial PIC
02 FILLER
PIC
01 NewName
PIC
01 Pointers.
02 StrPtr
PIC
02 UnstrPtr
PIC
88 NameProcessed
J ohn
X.
X(15).
X(30).
99 VALUE 1.
99 VALUE 1.
VALUE 81.
John
John
PROCEDURE DIVISION.
ProcessName.
ACCEPT OldName.
PERFORM UNTIL NameProcessed
UNSTRING OldName DELIMITED BY ALL SPACES
INTO TempName WITH POINTER UnstrPtr
END-UNSTRING
Display TempName
END-PERFORM
STOP RUN.
UNSTRING Example 7
01 OldName
Tim
PIC X(80).
John
Roberts
01 TempName.
02 NameInitial PIC
02 FILLER
PIC
01 NewName
PIC
01 Pointers.
02 StrPtr
PIC
02 UnstrPtr
PIC
88 NameProcessed
R oberts
X.
X(15).
X(30).
99 VALUE 1.
99 VALUE 1.
VALUE 81.
Roberts
Roberts
PROCEDURE DIVISION.
ProcessName.
ACCEPT OldName.
PERFORM UNTIL NameProcessed
UNSTRING OldName DELIMITED BY ALL SPACES
INTO TempName WITH POINTER UnstrPtr
END-UNSTRING
Display TempName
END-PERFORM
STOP RUN.
UNSTRING Example 8
OldName
Tim
John
Roberts
TempName
NewName
PROCEDURE DIVISION.
ProcessName.
ACCEPT OldName.
UNSTRING OldName DELIMITED BY ALL SPACES
INTO TempName WITH POINTER UnstrPtr
END-UNSTRING
PERFORM UNTIL NameProcessed
STRING NameInitial "." DELIMITED BY SIZE
INTO NewName WITH POINTER StrPtr
END-STRING
UNSTRING OldName DELIMITED BY ALL SPACES
INTO TempName WITH POINTER UnstrPtr
END-UNSTRING
END-PERFORM
STRING SPACE TempName DELIMITED BY SIZE
INTO NewName WITH POINTER StrPtr
END-STRING
STOP RUN.
UNSTRING Example 8
OldName
Tim
TempName
John
Roberts
T im
NewName
PROCEDURE DIVISION.
ProcessName.
ACCEPT OldName.
UNSTRING OldName DELIMITED BY ALL SPACES
INTO TempName WITH POINTER UnstrPtr
END-UNSTRING
PERFORM UNTIL NameProcessed
STRING NameInitial "." DELIMITED BY SIZE
INTO NewName WITH POINTER StrPtr
END-STRING
UNSTRING OldName DELIMITED BY ALL SPACES
INTO TempName WITH POINTER UnstrPtr
END-UNSTRING
END-PERFORM
STRING SPACE TempName DELIMITED BY SIZE
INTO NewName WITH POINTER StrPtr
END-STRING
STOP RUN.
UNSTRING Example 8
OldName
Tim
TempName
NewName
John
Roberts
T im
T.
PROCEDURE DIVISION.
ProcessName.
ACCEPT OldName.
UNSTRING OldName DELIMITED BY ALL SPACES
INTO TempName WITH POINTER UnstrPtr
END-UNSTRING
PERFORM UNTIL NameProcessed
STRING NameInitial "." DELIMITED BY SIZE
INTO NewName WITH POINTER StrPtr
END-STRING
UNSTRING OldName DELIMITED BY ALL SPACES
INTO TempName WITH POINTER UnstrPtr
END-UNSTRING
END-PERFORM
STRING SPACE TempName DELIMITED BY SIZE
INTO NewName WITH POINTER StrPtr
END-STRING
STOP RUN.
UNSTRING Example 8
OldName
Tim
TempName
NewName
John
Roberts
J ohn
T.
PROCEDURE DIVISION.
ProcessName.
ACCEPT OldName.
UNSTRING OldName DELIMITED BY ALL SPACES
INTO TempName WITH POINTER UnstrPtr
END-UNSTRING
PERFORM UNTIL NameProcessed
STRING NameInitial "." DELIMITED BY SIZE
INTO NewName WITH POINTER StrPtr
END-STRING
UNSTRING OldName DELIMITED BY ALL SPACES
INTO TempName WITH POINTER UnstrPtr
END-UNSTRING
END-PERFORM
STRING SPACE TempName DELIMITED BY SIZE
INTO NewName WITH POINTER StrPtr
END-STRING
STOP RUN.
UNSTRING Example 8
OldName
Tim
TempName
NewName
John
Roberts
J ohn
T.J.
PROCEDURE DIVISION.
ProcessName.
ACCEPT OldName.
UNSTRING OldName DELIMITED BY ALL SPACES
INTO TempName WITH POINTER UnstrPtr
END-UNSTRING
PERFORM UNTIL NameProcessed
STRING NameInitial "." DELIMITED BY SIZE
INTO NewName WITH POINTER StrPtr
END-STRING
UNSTRING OldName DELIMITED BY ALL SPACES
INTO TempName WITH POINTER UnstrPtr
END-UNSTRING
END-PERFORM
STRING SPACE TempName DELIMITED BY SIZE
INTO NewName WITH POINTER StrPtr
END-STRING
STOP RUN.
UNSTRING Example 8
OldName
Tim
TempName
NewName
John
Roberts
R oberts
T.J.
PROCEDURE DIVISION.
ProcessName.
ACCEPT OldName.
UNSTRING OldName DELIMITED BY ALL SPACES
INTO TempName WITH POINTER UnstrPtr
END-UNSTRING
PERFORM UNTIL NameProcessed
STRING NameInitial "." DELIMITED BY SIZE
INTO NewName WITH POINTER StrPtr
END-STRING
UNSTRING OldName DELIMITED BY ALL SPACES
INTO TempName WITH POINTER UnstrPtr
END-UNSTRING
END-PERFORM
STRING SPACE TempName DELIMITED BY SIZE
INTO NewName WITH POINTER StrPtr
END-STRING
STOP RUN.
UNSTRING Example 8
OldName
Tim
TempName
NewName
John
Roberts
R oberts
T . J . Roberts
PROCEDURE DIVISION.
ProcessName.
ACCEPT OldName.
UNSTRING OldName DELIMITED BY ALL SPACES
INTO TempName WITH POINTER UnstrPtr
END-UNSTRING
PERFORM UNTIL NameProcessed
STRING NameInitial "." DELIMITED BY SIZE
INTO NewName WITH POINTER StrPtr
END-STRING
UNSTRING OldName DELIMITED BY ALL SPACES
INTO TempName WITH POINTER UnstrPtr
END-UNSTRING
END-PERFORM
STRING SPACE TempName DELIMITED BY SIZE
INTO NewName WITH POINTER StrPtr
END-STRING
STOP RUN.
CALL Syntax
CALL Example.
CALL "DateValidate"
USING BY CONTENT TempDate
USING BY REFERENCE DateCheckResult.
IDENTIFICATION DIVISION.
PROGRAM-ID DateValidate IS INITIAL.
DATA DIVISION.
WORKING-STORAGE SECTION.
????????????
LINKAGE SECTION.
01 DateParam
01 DateResult
PIC X(8).
PIC 9.
CALL Parameters
CALL Parameters
Positions
Positions Correspond
Correspond -- Not
Not Names
Names
CALL .. BY
REFERENCE
CALLed
Program
CALL .. BY
REFERENCE
Direction
of Data Flow
CALLed
Program
CALL .. BY
REFERENCE
Direction
of Data Flow
CALL .. BY
CONTENT
CALLed
Program
CALLed
Program
Copy of
Data Item
CALL .. BY
REFERENCE
Direction
of Data Flow
Direction
of Data Flow
CALL .. BY
CONTENT
Data
Item
Copy of
Data Item
CALLed
Program
CALLed
Program
Address of
Copy
12
DATA
Total = 62
DATA DIVISION.
DIVISION.
WORKING-STORAGE
WORKING-STORAGE SECTION.
SECTION.
01
01 RunningTotal
RunningTotal PIC
PIC 9(7)
9(7) VALUE
VALUE 50.
50.
5
LINKAGE
LINKAGE SECTION.
SECTION.
01
PIC
01 ParamValue
ParamValue
PIC 99.
99.
Total = 55
PROCEDURE
PROCEDURE DIVISION
DIVISION USING
USING ParamValue.
ParamValue.
12
Begin.
Begin.
ADD
ADD ParamValue
ParamValue TO
TO RunningTotal.
RunningTotal.
Total = 62
DISPLAY
"Total
=
",
RunningTotal.
DISPLAY "Total = ", RunningTotal.
EXIT
EXIT PROGRAM.
PROGRAM.
Total = 67
PROCEDURE
PROCEDURE DIVISION
DIVISION USING
USING ParamValue.
ParamValue.
12
Begin.
Begin.
ADD
ADD ParamValue
ParamValue TO
TO RunningTotal.
RunningTotal.
DISPLAY
DISPLAY "Total
"Total == ",
", RunningTotal.
RunningTotal. Total = 79
EXIT
EXIT PROGRAM.
PROGRAM.
12
Total = 62
12
Total = 62
Contained Sub-Programs
$$ SET
SET SOURCEFORMAT"FREE"
SOURCEFORMAT"FREE"
IDENTIFICATION
IDENTIFICATION DIVISION.
DIVISION.
PROGRAM-ID.
MainProgram.
PROGRAM-ID. MainProgram.
?? ?? ?? ?? ?? ?? ?? ?? ??
01
GLOBAL
01 TableItem
TableItem IS
IS GLOBAL.
GLOBAL.
GLOBAL
PROCEDURE
PROCEDURE DIVISION.
DIVISION.
?? ?? ?? ?? ?? ?? ?? ?? ??
CALL
CALL PutToTable
PutToTable USING
USING BY
BY CONTENT
CONTENT DataItem
DataItem
?? ?? ?? ?? ?? ?? ?? ?? ??
CALL
CALL ReportFromTable.
ReportFromTable.
EXIT
EXIT PROGRAM.
PROGRAM.
IDENTIFICATION
IDENTIFICATION DIVISION.
DIVISION.
PROGRAM-ID.
PROGRAM-ID. PutToTable.
PutToTable.
?? ?? ?? ?? ?? ?? ?? ?? ??
END-PROGRAM
END-PROGRAM PutToTable.
PutToTable.
IDENTIFICATION
IDENTIFICATION DIVISION.
DIVISION.
PROGRAM-ID.
ReportFromTable.
PROGRAM-ID. ReportFromTable.
?? ?? ?? ?? ?? ?? ?? ?? ??
END-PROGRAM
END-PROGRAM ReportFromTable.
ReportFromTable.
END-PROGRAM
END-PROGRAM MainProgram.
MainProgram.
Contained Sub-Programs
$$ SET
SET SOURCEFORMAT"FREE"
SOURCEFORMAT"FREE"
IDENTIFICATION
IDENTIFICATION DIVISION.
DIVISION.
PROGRAM-ID.
MainProgram.
PROGRAM-ID. MainProgram.
?? ?? ?? ?? ?? ?? ?? ?? ??
01
GLOBAL
01 TableItem
TableItem IS
IS GLOBAL.
GLOBAL.
GLOBAL
PROCEDURE
PROCEDURE DIVISION.
DIVISION.
?? ?? ?? ?? ?? ?? ?? ?? ??
CALL
CALL PutToTable
PutToTable USING
USING BY
BY CONTENT
CONTENT DataItem
DataItem
?? ?? ?? ?? ?? ?? ?? ?? ??
CALL
CALL ReportFromTable.
ReportFromTable.
EXIT
EXIT PROGRAM.
PROGRAM.
IDENTIFICATION
IDENTIFICATION DIVISION.
DIVISION.
PROGRAM-ID.
PROGRAM-ID. PutToTable.
PutToTable.
?? ?? ?? ?? ?? ?? ?? ?? ??
END-PROGRAM
END-PROGRAM PutToTable.
PutToTable.
IDENTIFICATION
IDENTIFICATION DIVISION.
DIVISION.
PROGRAM-ID.
ReportFromTable.
PROGRAM-ID. ReportFromTable.
?? ?? ?? ?? ?? ?? ?? ?? ??
END-PROGRAM
END-PROGRAM ReportFromTable.
ReportFromTable.
END-PROGRAM
END-PROGRAM MainProgram.
MainProgram.
Contained Sub-Programs
$$ SET
SET SOURCEFORMAT"FREE"
SOURCEFORMAT"FREE"
IDENTIFICATION
IDENTIFICATION DIVISION.
DIVISION.
PROGRAM-ID.
MainProgram.
PROGRAM-ID. MainProgram.
?? ?? ?? ?? ?? ?? ?? ?? ??
01
GLOBAL
01 TableItem
TableItem IS
IS GLOBAL.
GLOBAL.
GLOBAL
PROCEDURE
PROCEDURE DIVISION.
DIVISION.
?? ?? ?? ?? ?? ?? ?? ?? ??
CALL
CALL PutToTable
PutToTable USING
USING BY
BY CONTENT
CONTENT DataItem
DataItem
?? ?? ?? ?? ?? ?? ?? ?? ??
CALL
CALL ReportFromTable.
ReportFromTable.
EXIT
EXIT PROGRAM.
PROGRAM.
IDENTIFICATION
IDENTIFICATION DIVISION.
DIVISION.
PROGRAM-ID.
PROGRAM-ID. PutToTable.
PutToTable.
?? ?? ?? ?? ?? ?? ?? ?? ??
END-PROGRAM
END-PROGRAM PutToTable.
PutToTable.
IDENTIFICATION
IDENTIFICATION DIVISION.
DIVISION.
PROGRAM-ID.
ReportFromTable.
PROGRAM-ID. ReportFromTable.
?? ?? ?? ?? ?? ?? ?? ?? ??
END-PROGRAM
END-PROGRAM ReportFromTable.
ReportFromTable.
END-PROGRAM
END-PROGRAM MainProgram.
MainProgram.
Pop
Pop
Pop
FD CommonFileArea IS EXTERNAL.
WORKING-STORAGE SECTION.
01 SharedRec IS EXTERNAL.
02 PartA
PIC X(4).
02 PartB
PIC 9(5).
ProgramB
ProgramC
01 SharedRec etc
ProgramD
01 SharedRec etc
Mike12345
SharedRec
WORKING-STORAGE SECTION.
01 SharedRec IS EXTERNAL.
02 PartA
PIC X(4).
02 PartB
PIC 9(5).
ProgramB
ProgramC
01 SharedRec etc
ProgramD
01 SharedRec etc
Mike12345
PUT
SharedRec
Mike12345
WORKING-STORAGE SECTION.
01 SharedRec IS EXTERNAL.
02 PartA
PIC X(4).
02 PartB
PIC 9(5).
ProgramB
ProgramC
01 SharedRec etc
ProgramD
01 SharedRec etc
Mike12345
Mike12345
PUT
SharedRec
Mike12345
GET
WORKING-STORAGE SECTION.
01 SharedRec IS EXTERNAL.
02 PartA
PIC X(4).
02 PartB
PIC 9(5).
CALL STATEMENT
Subroutines within a program are executed from the main program
SUB PROGRAMS that exist outside the main program can also
be executed from a program with the help of a CALL statement
Example of CALL
1
Calling Program
Identification Division.
.
.
Call SUB1
.
.
Call SUB2
.
.
.
Called programs
IDENTIFICATION DIVISION.
PROGRAM-ID. SUB1
.
.
GO BACK
3
IDENTIFICATION DIVISION.
PROGRAM-ID. SUB2
.
.
GO BACK
INSPECT
INSPECT
The INSPECT statement may be used for replacing a specific character in a
field with another character. It can also be used for counting the number
of occurrences of a given character
Identifier- 2 FOR
ALL
LEADING
CHARACTERS
Identifier 3
Literal 1
BEFORE
AFTER
INITIAL
Identifier 3
Literal 1
INSPECT FullName
FOR LEADING
TALLYING UnstrPtr
SPACES.
INSPECT Example 1
READ TextFile
AT END SET EndOfFile TO TRUE
END-READ
PERFORM UNTIL EndOfFile
PERFORM VARYING idx FROM 1 BY 1 UNTIL idx > 26
INSPECT TextLine TALLYING LetterCount(idx)
FOR ALL Letter(idx)
END-PERFORM
READ TextFile
AT END SET EndOfFile TO TRUE
END-READ
END-PERFORM
PERFORM VARYING idx FROM 1 BY 1 UNTIL idx > 26
DISPLAY "Letter " Letter(idx)
" occurs " LetterCount(idx) " times"
END-PERFORM
Modifying Phrases
LEADING
The LEADING phrase causes counting/replacement of all
Compare$il characters from the first valid one
encountered to the first invalid one.
FIRST
The FIRST phrase causes only the first valid character to
be replaced.
BEFORE
The BEFORE phrase designates as valid those characters
to the left of the delimiter associated with it.
AFTER
The AFTER phrase designates as valid those characters
to the right of the delimiter associated with it.
INSPECT Example 2
StringData
F F F F A F F F F F Q F F F Z
INSPECT Example 2
StringData
F F F F A G G G G G Q F F F Z
INSPECT Example 3
StringData
F F F F A F F F F F Q F F F Z
INSPECT Example 3
StringData
F F F F A G G G G G Q F F F Z
INSPECT Example 4
StringData
F F F F A F F F F F Q F F F Z
INSPECT Example 4
StringData
F F F F A G G G G G Q G G G Z
INSPECT Example 5
StringData
F F F F A F F F F F Q F F F Z
INSPECT Example 5
StringData
F F F F A G F F F F Q F F F Z
INSPECT Example 6
INSPECT StringData REPLACING
ALL "FFFF"
FFFF BY "FROG"
FROG
AFTER INITIAL "A" BEFORE INITIAL "Q".
StringData
F F F F A F F F F F Q F F F Z
INSPECT Example 6
INSPECT StringData REPLACING
ALL "FFFF"
FFFF BY "FROG"
FROG
AFTER INITIAL "A" BEFORE INITIAL "Q".
StringData
F F F F A F R O G F Q F F F Z
INSPECT Example 7
StringData
P X P F P D P T P P F X T D P
INSPECT Example 7
StringData
P y P z P b P a P P z y a b P
INSPECT Example 7
StringData
P X P F P D P T P P F X T D P
INSPECT Example 7
StringData
P X P F P D P T P P z y a b P
INSPECT Example 8
StringData
P x P f P d P T P P f x T d P
INSPECT Example 8
StringData
P y P s P z P T P P s y T z P
INSPECT Example 9
INSPECT CustAddress
CONVERTING "abcdefghijklmnopqrstuvwxyz"
abcdefghijklmnopqrstuvwxyz
TO
"ABCDEFGHIJKLMNOPQRSTUVWXYZ".
ABCDEFGHIJKLMNOPQRSTUVWXYZ
01 AlphaChars.
02 AlphaLower PIC X(26) VALUE
"abcdefghijklmnopqrstuvwxyz".
02 AlphaUpper PIC X(26) VALUE
"ABCDEFGHIJKLMNOPQRSTUVWXYZ".
INSPECT CustAddress
CONVERTING AlphaLower TO AlphaUpper.
AlphaUpper
INSPECT CustAddress
CONVERTING AlphaUpper TO AlphaLower.
AlphaLower
INSPECT Example 10
RussianPay
"R".
INSPECT Example 10
RussianPay
$12,345.67
"R".
INSPECT Example 10
RussianPay
R12,345.67
"R".
INSPECT Example 10
"R".
RussianPay
R12,345.67
R12,345.67 roubles
EXAMINE
BY character2
ALL
LEADING
UNTIL FIRST
FIRST
character1
EXAMINE
The EXAMINE verb
The second format is as given below:
ALL
LEADING
UNTIL FIRST
FIRST
REPLACING BY character2
character1
Sort
and
Merge
DATA DIVISION.
FILE SECTION.
FD StudentFile.
01 StudentDetails.
02 StudentId
02 StudentName.
03 Surname
03 Initials
02 DateOfBirth.
03 YOBirth
03 MOBirth
03 DOBirth
02 CourseCode
02 Grant
02 Gender
PIC 9(7).
PIC X(8).
PIC XX.
PIC
PIC
PIC
PIC
PIC
PIC
9(2).
9(2).
9(2).
X(4).
9(4).
X.
Sort Example.
FD
01
SalesFile.
SalesRec.
02 FILLER
PIC X(10).
SD WorkFile.
WorkFile
01 WorkRec.
02 WSalesmanNum
PIC 9(5).
02 FILLER
PIC X(5).
FD SortedSalesFile.
01 SortedSalesRec.
02 SalesmanNum
PIC 9(5).
02 ItemType PIC X.
02 QtySold
PIC 9(4).
PROCEDURE DIVISION.
Begin.
SORT WorkFile ON ASCENDING KEY WSalesmanNum
USING SalesFile
GIVING SortedSalesFile.
OPEN INPUT SortedSalesFile.
ENVIRONMENT
ENVIRONMENT DIVISION.
DIVISION.
INPUT-OUTPUT
INPUT-OUTPUT SECTION.
SECTION.
FILE-CONTROL.
FILE-CONTROL.
SELECT
SELECT WorkFile
WorkFile ASSIGN
ASSIGN TO
TO "WORK.TMP".
"WORK.TMP".
SD
SD WorkFile.
WorkFile.
01
01 WorkRecord.
WorkRecord.
02
02 ProvinceCode
ProvinceCode
02
SalesmanCode
02 SalesmanCode
02
02 FILLER
FILLER
PIC
PIC 9.
9.
PIC
9(5).
PIC 9(5).
PIC
PIC X(19).
X(19).
PROCEDURE
PROCEDURE DIVISION.
DIVISION.
Begin.
Begin.
SORT
SORT WorkFile
WorkFile ON
ON ASCENDING
ASCENDING KEY
KEY ProvinceCode
ProvinceCode
DESCENDING
KEY
DESCENDING KEY SalesmanCode
SalesmanCode
USING
USING UnsortedSales
UnsortedSales
GIVING
GIVING SortedSales.
SortedSales.
OPEN
OPEN INPUT
INPUT SortedSales.
SortedSales.
SortedSalesFile
Unsorted
Records
SORT
Process
Sorted
Records
WorkFile
SORT WorkFile ON ASCENDING KEY WSalesmanNum
USING SalesFile
GIVING SortedSalesFile.
Unsorted
Records
Unsorted
Hat
Records
SortedSalesFile
Sorted
Records
SORT
Process
SelectHatSales
WorkFile
SORT WorkFile ON ASCENDING KEY WSalesmanNum
INPUT PROCEDURE IS SelectHatSales
GIVING SortedSalesFile.
New Version
SelectHatSales.
SelectHatSales.
OPEN
OPEN INPUT
INPUT SalesFile
SalesFile
READ
READ SalesFile
SalesFile
AT
AT END
END SET
SET EndOfSales
EndOfSales TO
TO TRUE
TRUE
END-READ
END-READ
PERFORM
PERFORM UNTIL
UNTIL EndOfSales
EndOfSales
IF
IF HatRecord
HatRecord
RELEASE
RELEASE WorkRec
WorkRec FROM
FROM SalesRec
SalesRec
END-IF
END-IF
READ
READ SalesFile
SalesFile
AT
AT END
END SET
SET EndOfSales
EndOfSales TO
TO TRUE
TRUE
END-READ
END-READ
END-PERFORM
END-PERFORM
CLOSE
CLOSE SalesFile.
SalesFile.
Old Version
SelectHatSales
SelectHatSales SECTION.
SECTION.
BeginHatSales.
BeginHatSales.
OPEN
OPEN INPUT
INPUT SalesFile
SalesFile
READ
READ SalesFile
SalesFile
AT
AT END
END SET
SET EndOfSales
EndOfSales TO
TO TRUE
TRUE
END-READ
END-READ
PERFORM
PERFORM GetHatSales
GetHatSales UNTIL
UNTIL EndOfSales
EndOfSales
CLOSE
CLOSE SalesFile
SalesFile
GO
GO TO
TO SelectHatSalesExit.
SelectHatSalesExit.
GetHatSales.
GetHatSales.
IF
IF HatRecord
HatRecord
RELEASE
RELEASE WorkRec
WorkRec FROM
FROM SalesRec
SalesRec
END-IF
END-IF
READ
READ SalesFile
SalesFile
AT
AT END
END SET
SET EndOfSales
EndOfSales TO
TO TRUE
TRUE
END-READ.
END-READ.
SelectHatSalesExit
SelectHatSalesExit
EXIT.
EXIT.
ENVIRONMENT
ENVIRONMENT DIVISION.
DIVISION.
INPUT-OUTPUT
INPUT-OUTPUT SECTION.
SECTION.
FILE-CONTROL.
FILE-CONTROL.
SELECT
SELECT WorkFile
WorkFile ASSIGN
ASSIGN TO
TO "WORK.TMP".
"WORK.TMP".
SD
SD WorkFile.
WorkFile.
01
WorkRecord.
01 WorkRecord.
88
88 EndOfWorkFile
EndOfWorkFile
02
02 ProvinceCode
ProvinceCode
88
88 ProvinceIsUlster
ProvinceIsUlster
02
02 SalesmanCode
SalesmanCode
02
02 FILLER
FILLER
FD
FD UnsortedSales.
UnsortedSales.
01
01 FILLER
FILLER
FD
FD SortedSales.
SortedSales.
01
SortedRec.
01 SortedRec.
88
88 EndOfSalesFile
EndOfSalesFile
02
ProvinceCode
02 ProvinceCode
02
02 SalesmanCode
SalesmanCode
02
02 ItemCode
ItemCode
02
02 ItemCost
ItemCost
02
02 QtySold
QtySold
VALUE
VALUE HIGH-VALUES.
HIGH-VALUES.
PIC
PIC 9.
9.
VALUE
VALUE 4.
4.
PIC
PIC 9(5).
9(5).
PIC
PIC X(19).
X(19).
PIC
PIC X(25).
X(25).
VALUE
VALUE HIGH-VALUES.
HIGH-VALUES.
PIC
9.
PIC 9.
PIC
PIC 9(5).
9(5).
PIC
PIC 9(7).
9(7).
PIC
PIC 9(3)V99.
9(3)V99.
PIC
PIC 9(7).
9(7).
PROCEDURE
PROCEDURE DIVISION.
DIVISION.
Begin.
Begin.
SORT
SORT WorkFile
WorkFile
ON
ON ASCENDING
ASCENDING KEY
KEY ProvinceCode
ProvinceCode
SalesmanCode
SalesmanCode
INPUT
INPUT PROCEDURE
PROCEDURE IS
IS SelectUlsterRecs
SelectUlsterRecs
GIVING
SortedSales.
GIVING SortedSales.
OPEN
OPEN INPUT
INPUT SortedSales.
SortedSales.
SelectUlsterRecs.
SelectUlsterRecs.
OPEN
OPEN INPUT
INPUT UnsortedSales
UnsortedSales
READ
READ UnsortedSales
UnsortedSales INTO
INTO WorkRec
WorkRec
AT
END
SET
EndOfSalesFile
AT END SET EndOfSalesFile TO
TO TRUE
TRUE
END-READ
END-READ
PERFORM
PERFORM UNTIL
UNTIL EndOfSalesFile
EndOfSalesFile
IF
ProvinceIsUlster
IF ProvinceIsUlster RELEASE
RELEASE WorkRec
WorkRec
END-IF
END-IF
READ
READ UnsortedSales
UnsortedSales INTO
INTO WorkRec
WorkRec
AT
END
SET
EndOfSalesFile
AT END SET EndOfSalesFile TO
TO TRUE
TRUE
END-READ
END-READ
END-PERFORM
END-PERFORM
CLOSE
CLOSE UnsortedSales
UnsortedSales
SalesSummaryFile
Unsorted
Records
SORT
Process
Sorted
Records
Salesman
Summary
Record
SummariseSales
WorkFile
SORT WorkFile ON ASCENDING KEY WSalesmanNum
USING SalesFile
OUTPUT PROCEDURE IS SummariseSales.
SalesFile.
SalesFile.
SalesRec
PIC
SalesRec
PIC X(10).
X(10).
WorkFile.
WorkFile.
WorkRec.
WorkRec.
88
VALUE
88 EndOfWorkFile
EndOfWorkFile
VALUE HIGH-VALUES.
HIGH-VALUES.
02
PIC
02 WSalesmanNum
WSalesmanNum
PIC 9(5).
9(5).
02
PIC
02 FILLER
FILLER
PIC X.
X.
02
PIC
02 WQtySold
WQtySold
PIC X(4).
X(4).
FD
FD SalesSummaryFile.
SalesSummaryFile.
01
01 SummaryRec.
SummaryRec.
02
PIC
02 SalesmanNum
SalesmanNum
PIC 9(5).
9(5).
02
PIC
02 TotalQtySold
TotalQtySold
PIC 9(6).
9(6).
PROCEDURE
PROCEDURE DIVISION.
DIVISION.
Begin.
Begin.
SORT
SORT WorkFile
WorkFile ON
ON ASCENDING
ASCENDING KEY
KEY WSalesmanNum
WSalesmanNum
USING
USING SalesFile
SalesFile
OUTPUT
SummariseSales
OUTPUT PROCEDURE
PROCEDURE IS
IS SummariseSales.
SummariseSales.
SummariseSales
OPEN
OPEN INPUT
INPUT SalesSummaryFile.
SalesSummaryFile.
PERFORM
PERFORM PrintSummaryReport.
PrintSummaryReport.
SummariseSales.
SummariseSales.
OPEN
OPEN OUTPUT
OUTPUT SalesSummaryFile
SalesSummaryFile
RETURN
RETURN WorkFile
WorkFile
AT
AT END
END SET
SET EndOfWorkFile
EndOfWorkFile TO
TO TRUE
TRUE
END-RETURN
END-RETURN
PERFORM
PERFORM UNTIL
UNTIL EndOfWorkFile
EndOfWorkFile
MOVE
MOVE WSalesmanNum
WSalesmanNum TO
TO SalesmanNum
SalesmanNum
MOVE
MOVE ZEROS
ZEROS TO
TO TotalQtySold
TotalQtySold
PERFORM
PERFORM UNTIL
UNTIL WSalesManNum
WSalesManNum NOT
NOT == SalesmanNum
SalesmanNum
OR
OR EndOfWorkFile
EndOfWorkFile
ADD
ADD WQtySold
WQtySold TO
TO TotalQtySold
TotalQtySold
RETURN
RETURN WorkFile
WorkFile
AT
AT END
END SET
SET EndOfWorkFile
EndOfWorkFile TO
TO TRUE
TRUE
END-RETURN
END-RETURN
END-PERFORM
END-PERFORM
WRITE
WRITE SummaryRec
SummaryRec
END-PERFORM
END-PERFORM
CLOSE
CLOSE SalesSummaryFile.
SalesSummaryFile.
Write
Read
StudentFile
Unsorted
Student
Records
SORT
Process
Sorted
Student
Records
GetStudentDetails
WorkFile
SORT WorkFile ON ASCENDING KEY WStudentId
INPUT PROCEDURE IS GetStudentDetails
GIVING StudentFile.
ENVIRONMENT
ENVIRONMENT DIVISION.
DIVISION.
INPUT-OUTPUT
INPUT-OUTPUT SECTION.
SECTION.
FILE-CONTROL.
FILE-CONTROL.
SELECT
SELECT StudentFile
StudentFile ASSIGN
ASSIGN TO
TO "SORTSTUD.DAT"
"SORTSTUD.DAT"
ORGANIZATION
IS
LINE
SEQUENTIAL.
ORGANIZATION IS LINE SEQUENTIAL.
SELECT
SELECT WorkFile
WorkFile ASSIGN
ASSIGN TO
TO "WORK.TMP".
"WORK.TMP".
DATA
DIVISION.
DATA DIVISION.
FILE
FILE SECTION.
SECTION.
FD
StudentFile.
FD StudentFile.
01
PIC
01 StudentDetails
StudentDetails
PIC X(32).
X(32).
SD
WorkFile.
SD WorkFile.
01
01 WorkRec.
WorkRec.
02
PIC
02 WStudentId
WStudentId
PIC 9(7).
9(7).
02
FILLER
PIC
X(25).
02 FILLER
PIC X(25).
PROCEDURE
DIVISION.
PROCEDURE DIVISION.
Begin.
Begin.
SORT
SORT WorkFile
WorkFile ON
ON ASCENDING
ASCENDING KEY
KEY WStudentId
WStudentId
INPUT
PROCEDURE
IS
GetStudentDetails
INPUT PROCEDURE IS GetStudentDetails
GIVING
GIVING StudentFile.
StudentFile.
STOP
RUN.
STOP RUN.
GetStudentDetails.
GetStudentDetails.
DISPLAY
DISPLAY "Enter
"Enter student
student details
details using
using template
template below."
below."
DISPLAY
"Enter
no
data
to
end.".
DISPLAY "Enter no data to end.".
DISPLAY
DISPLAY "NNNNNNNSSSSSSSSIIYYMMDDCCCCGGGGS".
"NNNNNNNSSSSSSSSIIYYMMDDCCCCGGGGS".
ACCEPT
WorkRec.
ACCEPT WorkRec.
PERFORM
PERFORM UNTIL
UNTIL WorkRec
WorkRec == SPACES
SPACES
RELEASE
WorkRec
RELEASE WorkRec
ACCEPT
ACCEPT WorkRec
WorkRec
END-PERFORM.
END-PERFORM.
MERGE Syntax.
e.g.
MERGE WorkFile ON ASCENDING KEY StudentId
USING InsertionsFile, StudentFile
GIVING NewStudentFile.
ENVIRONMENT
ENVIRONMENT DIVISION.
DIVISION.
INPUT-OUTPUT
INPUT-OUTPUT SECTION.
SECTION.
FILE-CONTROL.
FILE-CONTROL.
SELECT
ASSIGN
SELECT StudentFile
StudentFile
ASSIGN TO
TO "STUDENTS.DAT"
"STUDENTS.DAT"
ORGANIZATION
IS
LINE
SEQUENTIAL.
ORGANIZATION IS LINE SEQUENTIAL.
SELECT
InsertionsFile
SELECT InsertionsFile ASSIGN
ASSIGN TO
TO "TRANSINS.DAT"
"TRANSINS.DAT"
ORGANIZATION
IS
LINE
SEQUENTIAL.
ORGANIZATION IS LINE SEQUENTIAL.
SELECT
NewStudentFile
SELECT NewStudentFile ASSIGN
ASSIGN TO
TO "STUDENTS.NEW"
"STUDENTS.NEW"
ORGANIZATION
IS
LINE
SEQUENTIAL.
ORGANIZATION IS LINE SEQUENTIAL.
SELECT
ASSIGN
SELECT WorkFile
WorkFile
ASSIGN TO
TO "WORK.TMP".
"WORK.TMP".
DATA
DATA DIVISION.
DIVISION.
FILE
SECTION.
FILE SECTION.
FD
FD StudentFile.
StudentFile.
01
StudentRec
01 StudentRec
FD
FD InsertionsFile.
InsertionsFile.
01
InsertionRec
01 InsertionRec
FD
FD NewStudentFile.
NewStudentFile.
01
NewStudentRec
01 NewStudentRec
SD
SD WorkFile.
WorkFile.
01
WorkRec.
01 WorkRec.
02
02 WStudentId
WStudentId
02
FILLER
02 FILLER
PIC
PIC X(32).
X(32).
PIC
PIC X(32).
X(32).
PIC
PIC X(32).
X(32).
PIC
PIC 9(7).
9(7).
PIC
X(25).
PIC X(25).
PROCEDURE
PROCEDURE DIVISION.
DIVISION.
Begin.
Begin.
MERGE
MERGE WorkFile
WorkFile ON
ON ASCENDING
ASCENDING KEY
KEY WStudentId
WStudentId
USING
InsertionsFile,
StudentFile
USING InsertionsFile, StudentFile
GIVING
GIVING NewStudentFile.
NewStudentFile.
STOP RUN.