100% found this document useful (5 votes)
1K views131 pages

PL-1 - Cobol Learning Mainframe

PL -1 Mainframe programming language and difference between Cobol and PL- 1 is captured in this document. Happy Learning.

Uploaded by

DGS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
100% found this document useful (5 votes)
1K views131 pages

PL-1 - Cobol Learning Mainframe

PL -1 Mainframe programming language and difference between Cobol and PL- 1 is captured in this document. Happy Learning.

Uploaded by

DGS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 131

PL/1

• PL/1 (Programming Language / 1)

PL/1 is a procedural, imperative computer programming language designed


for scientific, engineering, business and system programming applications.
It has been used by various academic, commercial and industrial users
since it was introduced in the 1960s.
PL/I ‘s principal domain is data processing; it supports numerical
computation, scientific computing, recursion, structured
programming, linked data structure handling, string handling, and bit
string handling. The language syntax is English-like and suited for
describing complex data formats, with a wide set of functions available to
verify and manipulate them.
PL/1 Statements & Programs
FORMAT OF A PL/1
STATEMENT
• column 2 to 72 are used for coding PL/1 statements. each
statement should end with a semicolon.

• first statement of a program should be PROCEDURE or


PROC. each procedure statement must begin with a label,
1 to 31 chars.

• the first character of a label should always be an


alphabet and the label should be followed by a colon.

• last statement of a procedure always ends with END. END


ends the program and passes the control to the operating
system.
FORMAT OF A PL/1 Statement
• form :- label:procedure options(main);
• e.g. :- run221a:proc options(main);

• form :- label:end descriptor;


• e.g. :- fini:end run221a;

• descriptor should be same as the label at the front


of the procedure statement of this procedure.

• e.g. :- examp : procedure options(main);

.........
PL/1 statement
.........
end examp;
Declaring Data Items
DECLARE Statement
• DCL is the statement after proc which means DECLARE and
is used to reserve storage for data or variables
indicating the attributes of a variable.

• in PL/1 there are 2 basic data types :-


arithmetic data and string data (char strings)
DCL Arithmetic data items
• form :- DCL variable_name mode scale base precision

• mode of an arithmetic item is either real or complex.


default is real.

• scale is either fixed or float.

• base is either decimal or binary.

• precision means number of figures that a fixed point


data item can hold or in case of floating point, the
minimum number of valid digits to be regarded as
significant.
DCL Arithmetic data items
• e.g. :- 102.30 decimal fixed point

125.3E5 decimal floating point

• a floating point number consists of 2 parts fraction and


exponent.

e.g. :- 168325E20 = 168325 * (10**20)


DCL Binary data items
• binary numbers consists of 1 and 0. to indicate that a
constant is a binary, it is followed by a "B".

• e.g. :- 101110010B [ precision is (9,0) ]

• a binary fixed point variable is declared as

DCL mbinnum fixed bin(16,0);

• the maximum number of digits in a binary number is 31.


the default for fixed binary variable is 15,0.
DCL String data items
• string data can be either character or bit string. PL/1
recognises string data by the quotation mark which must
appear at the start and finish of each string.

• e.g. :- 'the world'

• to use quotation in character string, code two of them.

• e.g. :- don't as a char string is represented by


'don''t'
DCL String data items
• to repeat a character string use (n)'char string'

• e.g. :- (80)'RAJ' (RAJ will be repeated 80 times.)

• declaration of character string variable is done as DCL


name CHAR(15)

• to vary not only the context but also the length of a


character variable use the attribute VARYING or VAR.

• e.g. :- DCL addr CHAR(33) VARYING;

• the length can vary from 1 to 33 and no padding of


spaces is done, if the length is less than 33.
BIT strings
• bit strings are like char strings except that the digits
represent one bit (0 and 1). bit strings
must be enclosed in quotation marks.

• bit strings may too have varying attributes.

• e.g. of bit string constants '1011101001'B


'111'B
(24)'0'B

• e.g. of bit string variables

DCL switch BIT(1),


DCL bitstr BIT(320) VARYING;
Explanation of some symbols
used in Pl/1 program
• COLON : - follows a label

• COMMA , - separates list items

• SEMICOLON ; - ends a statement

• PARENTHESES () - encloses list items

• ASTERISK * - multiplication
Difference between a binary
number & a bit string
• a binary number has a value and is treated like a
number.

• a bit string has no value and is treated like a string


of bits.
DEFINED or DEF attribute
• it is useful to refer to same pieces of string data
(bit or char) by more than one name. it can be used
to overlay or sub-divide areas of string data.

• e.g. :- DCL idcode CHAR(8);


DCL area CHAR(3) DEF idcode;
idcode='A02B1506';
PUT LIST(area);

• base variable idcode occupies 8 bytes of storage.

• variable area defined over it starts at the same


address but it is only 3 bytes long.

• put writes out the first 3 characters of idcode


'A02'.
POSITION or POS attribute
• with POS, you can make the defined variable start at any
position of the base variable.

• e.g. :- DCL idcode CHAR(8),


part1 CHAR(3) DEF idcode,
part2 CHAR(3) DEF idcode POS(4),
part3 CHAR(2) DEF idcode POS(7);

• part1 overlays the first 3 characters of idcode; part2


is over characters 4,5,6; and part3 is over characters
7,8.
POSITION or POS attribute
• e.g. :- DCL whole BIT(10),
select BIT(3) DEF whole POS(5);

WHOLE='1000111010'B;

• contents of select are '111'B


PICTURE attribute & some
special characters
• variables which are to hold both types of data must be
declared using picture attribute or pic.

• 9 indicates numeric, V indicates decimal point.

• picture edit characters must be enclosed in quotation


marks.

• minus and decimal point use storage spaces. if the


number is non-negative, it is replaced by a blank.

• e.g. :- DCL number PIC 'S999V.99'


number = 123.45;

• in storage, the number look like +123.45


PICTURE attribute & some
special characters
• to edit character strings, you may use 3 edit characters
:-

X position may hold any character.


A position may hold alphabetic character or a
blank.
9 indicates that position may hold numeric
characters or space.

• e.g. :- DCL fielda pic '999X99';


fielda = '123.45';
INITIAL or INIT attribute
• to assign value to a variable at the start of the
program execution use the attribute initial or init.

• e.g. :- DCL switch BIT 1 INIT('1'B);


DCL whole FIXED DEC(5) INIT(0);
DCL title CHAR(13) INIT('Weekly News');
Examples of some constants
• '101110'B bit string

• .25638E20 decimal floating point number

• '34862' character string

• 101101B binary fixed point number

• 10387 decimal fixed point number


Expressions
IF, THEN , ELSE statement
• simplest if then else statement :-

• form :- IF expression THEN then-unit-expression;


ELSE else-unit-expression;

• e.g. :- if a=0 then counter = counter + 1;

• e.g. :- if A <= B then A=B;


else A=B+100;

• e.g. :- DCL switch BIT(1);


switch = A;

if switch then goto sub1;


IF, THEN, ELSE statement
• DO in an IF statement indicates start of a DO group
block.
• END in an IF statement indicates end of a DO group
block.

• e.g. :- x, y = 1;
if a > b then do;
x = 100;
y = 50;
end;

• e.g. :- if a > b then c = 100;


else do;
x = y;
c = z + 10;
end;
IF, THEN, ELSE statement
• if character strings of different lengths are
compared then the shorter one is padded on the right
with blank spaces.

• in case of bit strings; if length differs, the


shorter string is padded to the right with '0'B.

• NULL statement :- null statement consists of a


semicolon. it is used where logically no statement is
necessary.

• e.g. :- if x > y then if m = n then;


else z = 0;
else z = z + 1;
OPERATORS
• comparison operators are as follows :-

< less than ¬< not less than


> greater than ¬> not greater than
= equal to ¬= not equal to
<= less than or equal to >= greater than or
equal to

• Bit string operators are as follows :-

NOT ¬ alt-170
AND &
OR |

• Concatenation character is ||
DO groups
DO group statement
• iterative do group used in an IF statement is also used
to put several PL/1 statements together,but in this type
of do group these statements are as a rule executed
several times over a group.

• e.g. :- do i = 1 to 10;
........
........
end;

the do group will run through 10 times.


DO group statement
• BY statement :- if the control variable is to be
increased by a step other than 1, then you use by
statement.

e.g. :- do count = 1 to 20 by 2;

• in do groups you can also count downwards.

e.g. :- do i = 25 to 1 by -1;

• in addition to constants; variables and expressions can


also be written in specification.

e.g. :- do alpha = x to beta * 2 by delta;


DO group statement
• Multiple Specification :- multiple specifications can be
used in a do statement. control passes to the next
specification when previous has finished.

e.g. :- do i = 1 to 5, 20 to 25, 50;

this loop is executed 5 times for i = 1 to


5, then 6 times for i = 20 to 25, and then
again with i = 50. hence the loop is
executed totally 12 times.
DO group statement
• often do groups have to be executed until a certain
condition is met. these cases can be handled with while
statement.

• e.g. :- do while (x < y);

• this loop is executed as long as x is less than y.

• e.g. :- do while (x > y) until (z = 100);


.....
endo;

• this loop runs as long as x > y and z is not equal to


100.
DO group statement
• nested DO groups :- nesting of do groups to a maximum of
49 is allowed by PL/1 optimizing compiler.

• e.g. :- outer : do i = 1 to 5;
inner : do j = 1 to 3;
.....
end inner;
end outer;
DO group statement
• e.g. :- to leave an iterative do group.

do i = 1 to 100;
.....
if x > y then leave;
.....
end;
GOTO statement
• with goto statement you can jump to any statement
which has a label, except an iterative do group.

• e.g. :- i = 1;
a:if i > 10 then goto b;
......
i = i + 1;
goto a;
b: .....
Data Aggregates & Arrays
Data Aggregates
• two types of data aggregates are there :-

- arrays

- structures

• an array is a collection of several elements with same


attributes. they are arranged in a table or a matrix.

• when you would like to put unequal data elements


together in an aggregate, you should use a structure.
Data Aggregates (Arrays)
• e.g. :- dcl table(50) fixed decimal(3);

• table is a one dimensional array of 50 elements. each


element is a decimal fixed point with the precision 3,0.
table is to be regarded as a string of 50 elements.

• if you want to address a single array element.

• e.g. :- dcl countytab(50) char(10);


dcl countynm char(10);

countynm = countytab(2);

• second element of countytab is assigned to countynm.


Data Aggregates (Arrays)
• e.g. :- DCL numbers(5) fixed dec(3);
do i = 1 to 5;
numbers(i) = i * 2;
end;

• subscripts need not just consist of constants and


variables, they can also be whole expressions.

e.g. :- cost(i+j)
numbers(i+2)
numbers(costs(i))
costs(i+j*2)
Data Aggregates (Arrays)
• Built-in functions :-
------------------
• a built-in function replaces a number of statements
which the programmer would otherwise have to write
himself.

• for e.g. function ALWAYS return one value and is


recognised by PL/1 thru syntax.

• e.g. :- dcl (a,b) fixed dec(9);


dcl x(10) fixed dec(3);
dcl y(12) fixed dec(7);
a = b + sum(x) + sum(y);
Data Aggregates (Arrays)
• Multi-Dimensional Array :-
-----------------------
• the dimension attribute states the number of the
dimensions for an array and the bounds of each
dimension.

• e.g. :- dcl table(7,2) fixed decimal(3);

• the above mentioned array consists of 7 rows and 2


columns.

• e.g. :- dcl parts(3,3,5) char(10);

• the above declared parts array consist of 45 elements.


Data Aggregates (Arrays)
e.g. :- dcl tab(3,5) fixed binary(15);

do j = 1 to 3;
do k = 1 to 5;
tab(j,k) = 2;
end;
end;

x = sum(tab);

• value of 'x' after execution is 30 because each of


the 15 elements in this array is set to 2.

• all element of array tab can be set to 2 by one


single instruction. [ tab = 2 ].
Data Aggregates (Arrays)
• Cross section of an array :- you can also address a
cross section of a array.

• e.g. :- dcl figures(5,3) fixed dec;


dcl x(3);
x = figures(2,*);

• in the above case * represents all the elements of row


2.

• hence x = figures(2,*) is equal to

x(1) = figures(2,1);
x(2) = figures(2,2);
x(3) = figures(2,3);
Data Aggregates (Structures)
• Simple Structures :-
-----------------
• when you would like to put unequal data elements
together in an aggregate, you should use a structure.

• e.g. :- dcl 1 personnel,


2 name char(30),
2 salary fixed decimal(7,2);

• name of the whole structure is personnel. each element


in it, has it's own name and it's own attribute.
Data Aggregates (Structures)
• during processing elements can be addressed singly or
all together using the structure name. a single element
can be addressed by it's name.

• for e.g. :- name = 'John Miller';

put file(out) list(personnel);


put file(out) list(name, salary);

• the above two statements have the same meaning. write to


the file out the values of name and salary.

• maximum number of levels in a structure is 15.


Data Aggregates (Structures)
• Arrays in a Structure & Qualified Names :-
---------------------------------------
• dcl 1 month(12),
2 max fixed dec(7,2),
2 min fixed dec(5,2);

• there are 12 occurence of month and each occurence


has 2 elements.

• if you want to refer the element min in 10th


structure.

month(10).min (OR) month.min(10)

• qualified name is month(10).min (OR) month.min(10)


BY NAME option in a structure
• there is one way by which you can use non-identical
structures in structure expression. that is by name
option.

• by name is used to make assignments from one


structure to another, whereby compiler processes only
the elements whose fully qualified names in both
structures are identical.

e.g. :- dcl 1 new, dcl 1 old,


2 name, 2 name,
2 address,
2 payment, 2 payment,
3 gross, 3 gross,
3 net, 3 net,
3 deductions; 2 deductions;
BY NAME option in a structure
• there is an assignment for all the elements which have
the same qualified names in the structures new and old.

new = old, by name;

• 3 elements are assigned in this structure expression.

new.name = old.name;
new.payment.gross = old.payment.gross;
new.payment.net = old.payment.net;
Introduction to
Input/Output
Basic features of Input/Output
• GET :- transmits or gets data from external medium
to internal storage.

• PUT :- transmits data from internal to external


storage.

• 2 types of transmission are there between internal &


external storage :-

stream i/o with get & put :- data item is transmitted


singly. advisable for small quantities i.e. screen &
card readers.

record input/output with read & write :- complete


record of data items is transmitted and copy of the
record is usually kept in internal storage. it is
preferable for larger qty of data.
Basic features of Input/Output
• syntax of 2 i/o statements :-
--------------------------
stream -> get file(input) list(a,b,c);

record -> read file(input) into(inarea);

• opening & closing of files :-


--------------------------
e.g. :- dcl master file record sequential;
..........
open file(master) output;
..........
close file(master);
Environment attribute
• environment attribute defines the physical
organisation of data sets (e.g. record length,
blocks) and the method of processing them.

• default value for the type of organization is


consecutive. this type of organization can be
processed only sequentially.

• e.g. :- dcl purchase record sequential input


env(consecutive);
open file(purchase);

• record is an attribute in the declaration.


stream is default for the transmission type.
STREAM Input/Output
STREAM I/O Features
• a string of characters is transmitted in stream i/o.

• character data type will not cause conversion if it


is used in get or put.

• standard files for stream i/o :- the get and the put
statements not having data file declarations are
linked with standard files for which there are
prescribed file names.

SYSIN for input, SYSPRINT for output

• hence PUT LIST(DATA LIST);


is taken as PUT FILE(SYSPRINT) LIST(DATA LIST);
3 Types of I/O Stream
• list - always requires data list

• data - requires no list

• edit - requires data and format list


STREAM I/O Features
• List-Directed I/O :-
-----------------
it is simplest form of stream I/O, and is ideal for
testing input at the terminal.

• e.g. :- get list(c1,c2,c3);


put list(gross,net,gross-net);

• e.g. :- dcl temps(7,2) fixed decimal(3);


.....
get list(temps);

14 elements would be read by the get statement.

• input in list directed i/o is in character form. the


data items are separated by comma and/or blanks.
STREAM I/O Features
• Data-Directed I/O :-
-----------------
input data can be read in the form of assignment
statements in input stream. a semi colon or the end
of data file ends the transmission of an i/o
statement.

• e.g. :- A=2, B='XY'; ------> get data(a,b);


A=5; ------> get data(a,b);
B='UV'; ------> get data(a,b);

• in first read 'A' is numerical and 'B' is character.


on second get 'A' = 5 and 'B' is unchanged.
on third get 'A' is unchanged and 'B' is 'UV'.
STREAM I/O Features
• Edit-Directed I/O :-
-----------------
• it is the most commonly used type of stream i/o. a
data list is needed here as well, to contain the
names of data items, arrays or structures. in
addition a format list is required in which the
format of i/o data is described.

• e.g. :- put file(output) edit(data list) (format);


get file(sysin) edit(data list) (format);

• e.g. :- get edit(qty, price, text)


(f(3),x(6),f(5,2),x(5),a(10));

F is used for numeric data item,


A for character string, X for space.
PAGE & SKIP in EDIT Directed
I/O
• for preparation of numerical data the format item P
can be used with all picture specification.

• e.g. :- dcl no fixed dec(5),


order fixed dec(7,2),
text char(20);

put PAGE edit('Revision List') (A);


put SKIP(2) edit(no,order,text)
(f(5), x(2), P'(4)Z9V.99-', x(2), a);

• after advancing to next page, a title is


printed. then two lines are skipped and the three
variables are printed with 2 spaces in between.

result is fffff ppppp.pp aaaaaaaaaaaaaaaaaaaa


LINESIZE & PAGESIZE
in EDIT-Directed I/O
• print is a file attribute which can be used in
connection with stream output.

• some options which can be stated in open statement


are :-

option meaning default


------ ------- -------
linesize(nn) line length 120 chars per line
pagesize(nn) page length 60 lines per page

• e.g. :- open file(list) output stream print


linesize(100) pagesize(50);
ON ENDPAGE condition
• if the option PAGESIZE(60) is in force and 60 lines
have already been printed then the on endpage
condition is raised before printing the 61st line and
the control passes to the most recently executed
on endpage statement.

• on endpage(sysprint) begin;
n = n + 1;
put edit('PG-NO', n) (page, X(110), A, P'ZZ9');
: : : :
: : :print N’s value
: : print char string
skip a page : 'PG-NO'
leave 110 blank spaces

• current page number is printed on the first line.


ON ERROR condition
• ON ERROR condition :-
------------------
PUT statements without data list, write out all the
variables of a program block. they give programmers
an excellent tool to use in searching for tools.

• e.g. :- on error begin;


on error system;
put data;
goto exit;
end;

• the on error condition must also have been run thru


atleast once before it can be executed.

• on error system means that in case of an error, the


system routine is executed that includes breaking off
the program execution.
Record Input/Output
Overview & Differences between
STREAM & RECORD I/O
• in record i/o, the data in a data set is considered
as a collection of record.
in stream i/o, data is considered as a continuous
sequence.

• record i/o transmits records just as they are stored.


in stream i/o data conversion is possible.

• in record i/o, there are no standard files.


SYSIN and SYSPRINT are valid standard files for
stream i/o.

• record i/o allows various forms of data set


organization.
stream i/o allows only sequential form of data set.
RECORD I/O Features
• record i/o offers 2 alternative modes of processing
(move / locate).

• MOVE mode :- in move mode, a logical record is


transmitted by reading from the buffer to an area
(e.g. structure) where it is then processed.

• form :- read file(filename) into(area);

• using read, a logical record is put into a variable


declared in the program.

e.g. :- dcl 1 inrec,


2 part1 char(25),
2 part2 (10) decimal fixed(7,2);

read file(material) into(inrec);


RECORD I/O Features
• IGNORE option :- ignore specifies how many logical
records are to be skipped.

• form :- read file(filename) ignore(N);

'N' is the number of records to be skipped.

• Record output in MOVE mode:- on each write, a


logical record is transmitted from the area to a
buffer. if record (or block) is full, a physical
record (for block) is written into the data set.

form :- write file(filename) from(area);

write statement causes a record to be


written from the area specified i.e. the
records are put in blocks.
RECORD I/O Features
• declaration and opening of file :-
-------------------------------
• e.g. :- dcl pers file input sequential buffered
env(consecutive FB RECSIZE(100)
BLKSIZE(500) BUFFERS(3));

FB means fixed record length, blocked


RECSIZE means length of the record in bytes
BLKSIZE means length of the block in bytes
BUFFERS means number of buffers

• the record has 100 characters and block has 500


characters, hence there are 5 records per block.

• environment attribute describes the physical


properties of a data set. consecutive means that
records are stored sequentially.
LOCATE mode
• in this mode the logical records are directly
processed in buffer. here a based variable or a
pointer variable is needed (a structure with the
length of a logical record) is like a mask which is
laid over the buffer.

• record input in locate mode :- files to be processed


using locate mode must, like other files be declared
and opened. the first physical record has already
been read into the buffer and opened.

• using :- read file(filename) set(pointer);

• the address of the first record logical pointer is


put in the variable pointer.
POINTER Variable
• a pointer variable is a variable (full word, 4 bytes)
which contains a storage address. this address can be
assigned to pointer variable in various ways.

• e.g. :- dcl xyz pointer;


read file(input) set(xyz);

• a pointer is used to point to the position of the next


record in a buffer or to another variable.
BASED Variable
• in the e.g. given below, read places the address of next
logical record in the pointer ptr.

• e.g. :- dcl 1 efile based(ptr),


2 name char(30),
2 first_name char(20),
2 persno fixed dec(5);

read file(pers) set(ptr);

• the based variable efile is overlaid on the logical


record, as it has the attribute based and contains
address as the pointer.
Example of Record read in locate
mode
• dcl one file input sequential buffered
env(fb recsize(200) blksize(1000) buffers(1));
dcl q pointer;
dcl record_a char(200) based(q);
.....
read file(one) set(q);

• on 6th read, the 2nd physical record is retrieved from


the data set and put in buffer.

• you recognize MOVE mode by the option FROM and


LOCATE mode by the option SET.
Record Output in Locate mode
Records read in are written out
• form :- LOCATE based_variable FILE(filename);

• e.g. :- dcl in file record buffered input


env(fb recsize(100) blksize(1000)
buffers(2));
dcl out file record buffered output
env(fb recsize(100) blksize(1000)
buffers(2));

dcl inrec char(100) based(q);


dcl outrec char(100) based(p);

loop : read file(in) set(q);


locate outrec file(out);
outrec = inrec;
goto loop;
Rewriting of a file in MOVE and
LOCATE mode
• with REWRITE statement, records read in can be
changed and written back to the same place they came
from in the data set.

• e.g. :- dcl personnel file sequential buffered


update;
dcl record char(500) based(p);
dcl p pointer;

read file(personnel) set(p);


.......
change record read in .....
.......
rewrite file(personnel);
Variable length record
• if quantity of data per logical record varies a great
deal (as for e.g. with addresses) it is a good idea
to store the data in variable length record.

• note :- there is 4 byte record length field (RL)


before every variable record.
there is a 4 byte block length field before
every block.

• e.g. :- suppose the biggest logical record can be


1000 bytes long and that the records are to
be blocked in such a way that a block is
likewise to have the maximum length of 1000
bytes, then environment attribute of file
declaration would look like

env(vb recsize(1004) blksize(1008));


Variable length record
• size statements are calculated as :-

recsize = 1000 bytes + 4 bytes record length


blksize = recsize + 4 bytes block length

• in fixed format, the record length and the block


length are fixed but in variable format, the record
length and the block length vary.

block length is size of the physical record


record length is size of the logical record

if data length of the record is 750, the record


size will be 754 and the block size will be 758.
SCALARVARYING option
• if you are using record i/o with variable length
strings and locate mode, then scalar varying option must
be stated in env attribute for i/o, so that the 2 byte
length area which goes before each variable length
string are taken into consideration.

• e.g. :- dcl pers-file file record output buffered


env(vb recsize(106) blksize(110)
scalarvarying buffers(1));

dcl outrec char(100) varying;


Handling VSAM Files
VSAM data sets
• 3 types of VSAM data sets are there :-

- key sequenced data set (KSDS)


- entry sequenced data set (ESDS)
- relative record data set (RRDS)

• note :- sequential is the default file organization,


hence vsam should be specified in the ENV option.

• vsam files are always record files. records on a vsam


file are not held in blocks, hence the blksize option
even if specified is ignored. a recsize
option will be checked against the details stored
with the file.

• the utility IDCAMS (access method services) is used


to set up a vsam file initially.
Various DCL for VSAM datasets
• e.g. :- dcl vfile file record direct keyed input
env(vsam);

vsam files are all direct access files. associated


with every record is a key, which helps in accessing
the record in any order.

• e.g. :- dcl vfile file record sequential input


env(vsam);

if you want to start at the beginning and process


every record, just as with a consecutive file,
specify the file as sequential.
Various DCL for VSAM datasets
• e.g. :- dcl vfile file record sequential keyed input
env(vsam);

the first read of the program specifies the key of


the record you want to start at. subsequent
sequential read statements are issued without a key.

• e.g. :- dcl infile file record direct keyed input


env(vsam);
to read a vsam file directly.

• e.g. :- dcl infile file record direct update


env(vsam password('master'));

when a file is originally set, it can be given one or


more passwords. if attribute record is given, it
means that it must be a file, hence the attribute
file is implied and therefore optional.
Key Sequenced Data Set (KSDS)
• KSDS is used to hold master file data. identification
is by means of key so the attribute DIRECT implies
KEYED.

• e.g.:- dcl amast file record direct input env(vsam);

• sequential access is possible with ksds.

• a ksds has a key which is part of a data record, this


is known as an embedded key.

• keys are also stored in another part of data set


called as prime index.

• the keys are unique since duplication is not allowed.


KSDS Features
• Reading a ksds directly :- records are accessed by
specifying a value using the key option.

e.g.:- dcl amast file record direct input env(vsam);


dcl 1 area,
2 kskey char(5),
2 ksname char(15);
read file(amast) into(area) key('00010');

• Updating a ksds directly :- the file should be opened


as update. you cannot amend the key.

e.g.: dcl kmast file record direct update env(vsam);


dcl 1 area,
2 kskey char(5) init('00081'),
2 ksname char(15);
read file(kmast) into(area) key(kskey);
/* amend */
rewrite file(kmast) from(area) key(kskey);
KSDS Features
• Delete a record in ksds :- use delete statement.

e.g. :- kskey = '00056';


delete file(ksds) key(kskey);

• Add a record in ksds :- use write statement.

e.g. :- kskey = '00024'; ksname = 'Applets';


write file(ksds) from(area) keyfrom(kskey);

• First direct access & then sequential access :-

e.g.: dcl ramp file record sequential keyed


env(vsam);

read file(ramp) into(area) key('123/999/b');

read file(ramp) into(area);


GENKEY option
• e.g. :- dcl vsam file record sequential keyed
env(vsam genkey);
read file(vmast) into(area) key('123');
read file(vmast) into(area);

assume that the key is defined as nine chars long.

if genkey option is not specified in the environment


statement, vsam searches for a record with a key of
‘123 ‘.

if genkey option is specified the first read reads the


first record of data set beginning with 123.

second read continues with the record immediately


following it.
ENTRY Sequenced Data Set (ESDS)
• it is the most likely consecutive non-vsam file. i.e. data
is not in a particular order, hence the file is sequential.

• an esds file which already has data on it, may only be


opened with either input or update attributes.

e.g. :- dcl esds file record sequential update env(vsam);

• updating an esds file :- it is possible to change records


once they have been written to esds file.

e.g. :- read file(esds) into(area);


......
rewrite file(esds) from(area);

• write can also be used and the new records added are always
added at the end.

• delete is not allowed.


Keyed access to an ESDS file
• an esds is a sequential file and has no defined key.

• the information that can be used for keyed access is called


relative byte address (rba).

record-1 : record-2 : record-3 : record-4


: : :
50 bytes : 40 bytes : 60 bytes : 30 bytes
: : :
rba=0 rba=50 rba=90 rba=150

• you have to get hold of rba by using KEYTO option on read


and write.

• e.g. :- dcl esds file record sequential keyed update


env(vsam);
dcl rba char(4);
......
write file(esds) from(area) keyto(rba);
Keyed access to an ESDS file
rba must be a 4 byte character string. this will add a new
record to the end of the file and place the relative byte
address of the record in the field rba. this is then used
as a key on a read or rewrite. file should be declared as
keyed.

e.g. :- read file(esds) into(area) key(rba);

• e.g. :- /* example of esds read and rewrite */


dcl esds file record sequential keyed update
env(vsam);
dcl rba char(4);
read file(esds) into(area) keyto(rba);
. . . . . .
rewrite file(esds) from(area) key(rba);
RELATIVE Record Data Set (RRDS)
• records are read and can be accessed sequentially or
directly with or without keys.

• the data set may be thought of as a set of numbered


slots each of which may hold record. the numbering
starts at 1 and goes to the maximum specified.

• record number is the relative record number. this is


the key to the file.

• the key must be declared as a character string of


length 8.

• e.g. :- dcl relrecno char(8) init('00000145');

• e.g. :- dcl relrecno char(8) init(' 145');


Keyed access to an RRDS
• file may be declared as sequential or direct.

• if the file is declared as keyed sequential all key options


can be used. if you leave key reference, processing would be
just for a sequential file.

dcl rrds file record sequential keyed update env(vsam);


dcl rrkey char(8);
rrkey = ('00000025');
read file(rrds) into(area) key(rrkey); /* read rel.rec.no 25 */
......
rewrite file(rrds) from(area); /* rec.no 25 is rewritten */
......
read file(rrds) into(area) keyto(rrkey); /* requests a */
/* record number. */
/* the next record will */
/* be presented and the */
/* key value will be */
/* placed in rrkey */
WRITE in an RRDS
• e.g. :- /* assume record 5 exists on file rrds. */

dcl rrds file record sequential keyed update


env(vsam);

dcl relrecno char(8);


dcl 1 area,
2 ...... ;

relrecno = '00000005';
read file(rrds) into(area) key(relrecno);
......
delete file(rrds) key(relrecno);
......
write file(rrds) from(area)
keyfrom(relrecno);
Processing of String Data
SUBSTR (Built-in Function)
• substr is used to access a part of a string.

• form :- (string-var., start-pos., length-of-extract)

• e.g. :- dcl data char(50);


dcl field char(10);
......
data='INTERNATIONAL BUSINESS MACHINES';
field=substr(data,6,8);

• field will be 'NATIONAL '

in substr parameter 2 and 3 can be expression,


resulting to be an integer.

• note :- if the parameters represent a value which


lies outside the string the result is unpredictable.
INDEX (Built-in Function)
• index function is used to look for a certain
character or a string of chars in a given string.

• form :- pos. = index(string, search string);

• index function returns a binary number (15,0).


if string to be looked for is not found, the return
value is zero. only first occurence is shown.

• e.g. :- dcl town_country char(25);


......
town_country = 'WEYBRIDGE,SURVEY';
icomma=index(town_country,',');

icomma will be 10.

e.g. :- dcl (string,part1) char(30);


......
ipos = index(string,'/');
part1 = substr(string,1,ipos-1);
VERIFY (Built-in Function)
• VERIFY function :- verify will see that a string will have
only certain characters present.

• form :- verify(x,y);

'X' is the string of chars to be checked in Y.


'Y' is the string containing the list of
characters which are to be looked for.

• a binary fixed point number is returned :-


- zero, if all characters in 'X' appear also in 'Y'.
- a value, which gives the position of the first
character in 'X' which does not appear in 'Y'.

• e.g. :- dcl test char(3);


test='BIT';
IX=verify(test,'ABCDEDFGHI');

3 would be returned, because the third character is not in


the second parameter of verify.
TRANSLATE (Built-in Function)
• form :- translate(x,y,z)

'X' = character string to be checked


'Y' = string of replacement character
'Z' = chars in 'X' which are to be replaced if found

result is modified character string 'X'.

• if a character is found in 'X' which appears in 'Z', then


the char in 'X' is replaced by character from 'Y' which is
the same position in 'Y' as is the found character in 'Z'.

• e.g. :- dcl (old, new) char(10);


dcl icharacter char(10) init('FIRTUX');
dcl tcharacter char(10) init('ANUIQT');
......
old = 'FIXTURES';
new = translate(old,tcharacter,icharacter);

• new = translate('FIXTURES','ANUIQT','FIRTUX'); = ANTIQUES


LENGTH (Built-in Function)
• this determines the length of the string.

• value returned is binary fixed point number (15,0).

• form :- length(x)

• e.g. :- dcl field char(90) varying;


field = 'GOOD LUCK !';
ilength = length(field);

• ilength receives the value 11.


Pseudovariables
• if a function is on the left in an assignment, you
have a pseudovariable. pseudovariables are functions
which may not only manipulate values, but may also
receive them.

• e.g. :- substr(tfield,2,5) = gfield;

• e.g. :- dcl x char(10);


x = 'ABCDEFGHIJ';
substr(x,6,5) = substr(x,1,5);

/* soln is 'ABCDEABCDE' */

• note :- sqrt(sum) = 2; is not a pseudovariable.


Program Organization
Types of Blocks
• PL/1 knows two types of blocks :-

1. Procedures
2. Begin-blocks

• Procedures are of two types :-

1. External Procedures
2. Internal Procedures

• an EXTERNAL procedure is a block which is not contained in


another block.

they are compiled separately but they can joined to make


one program by linkage editor, so that 2 programmers can
work on it simultaneously.

option main identifies the main procedure.


i.e. label:proc options(main);
Types of Blocks
• an INTERNAL procedure is invoked by a call from a
surrounding block and control returns to statement
after the call.

• within an external procedure you can have upto 50


internal procedures.

• e.g. :- outer:proc options(main);


statement1
call inner; --------
----------> statement2 :
: ^ inner:proc; <-------
: : statement3
: : statement4
V ------- end inner;
----------> statement5
statement6
end outer;
Types of Blocks
• BEGIN-block :- they always lie within a procedure and
are executed where they are.

• they finish with an end statement.

• form :- A:proc;
......
begin;
......
......
end;
......
end A;
Scope of Declaration
• Variables which are declared have two scope attributes :-

1. Internal
2. External

• if a variable has an INTERNAL scope, then it is known in


the block in which it was declared and in all the blocks
which physically lie in that block until it is not declared
in one of these subordinate blocks.

e.g. :- outer:proc options(main);


dcl rate fixed dec(7,3);
dcl minus char(5);
dcl x char(100);
......
inner:proc;
dcl minus fixed dec(7,2);
dcl x char(100);
end inner;
end outer;
Scope of Declaration
rate field is addressed in both blocks.

character field minus is not known in block inner.

the variable 'X' has attribute internal and hence you are
dealing with 2 variables that are known within their blocks.

• if a variable is to be same in two external procedures, then


it must be declared with same attributes EXTERNAL.

mpro:proc options(main); subpro:proc;


dcl 1 rec external, dcl 1 rec external,
2 a char(30), 2 a char(30),
2 b fixed bin(31); 2 b fixed bin(31);

structure rec which is declared in both external procedures,


occupies the same data area.
Storage Class in a Variable
• using storage class attributes, the programmer defines,
when storage space is to be allocated to a variable and
when this storage space is freed.

• there are 4 different storage class :-


- automatic (default)
- static
- based
- controlled
AUTOMATIC Storage Class
• an automatic variable has internal attribute.

storage is allocated when block containing storage


declaration is activated and is freed again when the block
is terminated.

e.g. :- x:proc options(main);


......
begin; <-------------------------
...... :
dcl tab(100,100,10,2) char(25); : activated
...... :
end; <------------------------- freed
......
end x;
STATIC Storage Class
• static variable has external attribute.

storage is allocated before the execution of the program


and remains allocated until end of the program.

this storage class cannot be influenced by the


programmer during program execution.

e.g. :- dcl tab(10,10) fixed decimal(7,2) static;


dcl x(10,10) fixed decimal(5,2) based(p);
dcl x char(120) static external;
BASED Storage Class
• based storage class is used in connection with record i/o.

based variable should always have attribute internal.

in locate mode, it was necessary to declare the storage


class based for record to be read in / written out.

e.g. :- dcl in file record buffered input


env(fb recsize(100) blksize(1000) buffers(2));
dcl out file record buffered output
env(fb recsize(100) blksize(1000) buffers(2));
dcl iarea char(100) based(q);
dcl oarea char(100) based(p);
dcl (q,p) pointer;
......
read file(in) set(q);
......
locate oarea file(out);
CONTROLLED Storage Class
• with controlled variable the programmer has complete
control over storage space.

using ALLOCATE space is assigned and with FREE the storage


space is freed again. they can have the attributes internal
or external.

e.g. :- dcl fieldx char(12) controlled;


allocate fieldx; /* storage space is assigned */
fieldx = 'all clear ?';
put file(printer) edit(fieldx) (x(5),A);
free fieldx; /* storage space is freed */
Subroutines & Functions
Subroutines
• it is always an internal or external procedure. a
subroutine is not replaced by a result value after its
execution. a subroutine is always invoked with a call
statement. subroutines are often added as external
procedures.

• e.g. :- mrou:proc options(main);


dcl ......;
call srou1;
......
call srou2;
end mrou;

srou1:proc; srou2:proc
dcl ......; dcl ......;
...… ......
end srou1; end srou2;
Arguments
• when a procedure is invoked using a call, it is possible
to pass variables which are called as arguments.

• e.g. :- dcl rec1 char(45);


dcl 1 inrec,
2 part1 char(10),
2 amount fixed dec(7,2);

call srou(rec1,inrec); /* arguments */


Parameters
• a program to which arguments are passed when it is invoked,
are called as parameters.

• e.g.:- srou:proc(a,b);
dcl a char(100);

dcl 1 b,
2 b1 char(10),
2 b2 fixed dec(7,2);

here a & b are the parameters where a = rec1 and b = inrec.

the sequence and the attributes of the arguments and


parameters must be identical. names may be different, but
they use same area.

note :- null arguments may be indicated by two commas and


unnecessary arguments of the end of the list may be omitted.
Multiple Entry Points
• subroutine may have several entry points.
- primary entry points can be identified by PROC.
- secondary entry points can be identified by ENTRY
statements.
each entry points may have different parameter list.
e.g. :- prog1:proc options(main);
call prog5(a,b);
call ent2(a,b,c);
call ent1(c);
end prog1;

proc5:proc(x,y);
......
ent1:entry(m);
......
ent2:entry(x,y,m);
......
end prog5;
Exit from Procedures
• normal exit for a procedure is END.

• furthur exit can be provided using RETURN.

• e.g. :- srou:proc(x,y);
......
ent2:entry(m,n,o);
......
if x = 0 then return;
......
end srou;
Functions
• functions are always a part of an expression. a function
is replaced by its result after execution.

• e.g. :- /* built-in functions */

x = sum(tab3); /* with an arument */

adds up all elements of array tab.

v = date(); /* with empty arguments */

• a function call is replaced by value returned.

• e.g. :- calc:proc(a,b,c);
......
if x > y then return(x*y);
else return(z);
......
end calc;
(ON-UNIT)
• on ENDPAGE and on ENDFILE are some important conditions
which can be forseen.

• e.g. :- on endpage(sysprint) begin;


n = n + 1;

put edit('page', n) (page, x(70), a, p'z9');


put skip(3);
end;

• this writes the current page number when the preceding


page is full.
SIGNAL & REVERT
• SIGNAL option :- with signal statement, a condition can be
caused artifically. the program continues with the
statement which comes immediately after signal statement.

form :- SIGNAL condition;

signal is used to raise conditions deliberately.

e.g. :- on endpage(print3) call overs;


......
signal endpage(print3);

• REVERT option :- cancels on-unit for the condition


specified.

e.g. :- on error call error1;


/* error1 will be called */
on error call error2;
/* error2 will be called */
revert error;
/* cancels error2, error1 will be called */
Sample Programs
Sample Program 1
run221a:proc options(main);

dcl alpha fixed decimal(7,0),


beta fixed decimal(6,0);
dcl (a,b,c) fixed decimal(5,2),
answer fixed decimal(7,2);

alpha = 100;
beta = alpha + 200;

on endfile(sysin) goto fini;

read:get file(sysin) list(a,b,c);


answer=a+b-c;
put file(sysprint) list(a,b,c,answer)
goto read;
fini:end run221a;
Sample Program 2
P1M0:proc options(main);
dcl counter fixed dec(1);
dcl total fixed dec(7,2);
dcl number fixed dec(5,2);

on endfile(sysin) goto finish;

counter=0;
total=0;

read:
get file(sysin) list(number);
total = total + number;
counter = counter + 1;
goto read;

finish:
put file(sysprint) list(counter,total);

end p1m0;
Sample Program 3
P3M0:proc options(main);
dcl (countin, countout) fixed dec(3) init(0);
dcl (totalin, totalout) fixed dec(7,2) init (0);
dcl (averagein, averageout) fixed dec(5,2);
dcl number fixed dec(5,2);

on endfile(sysin) goto finish;

read:get file(sysin) list(number);


if number > 100 & number <= 60; then goto in;
totalout = totalout + number;
countout = countout + 1;
goto read;

in: totalin = totalin + number;


countin = countin + 1;
goto read;
Sample Program 3 Contd …..
finish:
averagein = totalin / countin;
averageout = totalout / countout;

put file(sysprint) list(countin, totalin, averagein);


put file(sysprint) skip list(countout, totalout, averageout);

end;
Sample Program 4
P6M0:proc options(main);
dcl temp(7,2) fixed dec(3), da(7) fixed dec(3);
dcl wa fixed dec(3), i fixed bin(15), eof bit(1) init('0'B);

on endfile(sysin) eof='1'B;

get file(sysin) list(temp); /* seven pairs of temparature */


do while (eof = '0'B);
do i = 1 to 7;
da(i) = (temp(i,1) + temp(i,2)) / 2;
end;

put file(sysprint) skip list(da); /* write to sysprint */


put file(sysprint) skip list(wa); /* 7 values of da,wa */
get file(sysin) list(temp); /* new input of temp */
end;
end p6m0;
Sample Program 5
P9M0:PROC OPTIONS(MAIN);

dcl E1 file record env(F recsize(50), blksize(50));

dcl A CHAR(50) init('THIS IS A TEST');


write file(E1) from(A); /* output is written to file E1 */

close file(E1);

read file(E1) into(A); /* reads data from file E1 */

put data(A); /* write data item 'A' */

put skip list('END of JOB'); /* write to file sysprint */

end P9M0;
Sample Program 6
p11m0:
proc options(main);
dcl 1 indet,
2 intype char(1),
2 itemno char(5),
2 uprice fixed dec(3,2),
2 qty fixed dec(3),
2 deptno char(3),
2 code1 fixed dec(3),
2 code2 fixed dec(3),
2 code3 fixed dec(3),
2 iname char(23),
2 dname char(27);
Sample Program 6 Contd …..
dcl ab char(40);
dcl 1 sta based(addr(ab)),
2 rectype char(4),
2 itemno char(4),
2 uprice fixed dec(3,2),
2 qty fixed dec(3),
2 totval fixed dec(7,2),
2 iname char(23);

dcl 1 stb based(addr(ab)),


2 rectype char(4),
2 deptno char(3),
2 code1 fixed dec(3),
2 code2 fixed dec(3),
2 code3 fixed dec(3),
2 dname char(27);
Sample Program 6 Contd …..
dcl a1 file record env(fb recsize(40) blksize(400));
dcl (eofi, eofa) bit(1) init('0'b);
on endfile(sysin) eofi = '1'b;
open file(a1) output;
do until (eofi = '1'b);
read file(sysin) into(indet);
if ªeofi then do;
if intype = 'A' then do;
sta=indet, by name; sta.rectype = ‘fap7’;
sta.totval = indet.uprice * indet.qty;
end;
else do;
stb=indet, by name; stb.rectype = 'fbp7';
end;
write file(a1) from(ab);
end;
else close file(ab);
on endfile(a1) eofa = '1'b;
Sample Program 6 Contd …...
open file(a1) input;
do until (eofa);
read file(a1) into (ab);
if ªeofa then
if sta.rectype = 'fap7' then
put skip list(sta);
else put skip list(stb);
else close file(a1);
end;
end p11m0;
Sample Program 7
P14M0:
proc options(main);
dcl ca char(78);
dcl (name,street,town) char(25);
dcl 1 prline,
2 cc char(1) init(' '),
2 text char(25);
dcl (del1,del2,del3) fixed bin(15);
dcl (sn,ss,st) fixed bin(15);
dcl (ln,ls,lt) fixed bin(15);

do until (eof);
read file(sysin) into(ca);
if ªeof then do;
del1=index(ca,',');
del2=index(substr(ca,del1+1),',') + del1;
del3=index(substr(ca,del2+1),',') + del2;
Sample Program 7 Contd …..
sn=1;
ss=del1+1;
st=del2+1;

ln = del1 - sn;
ls = del2 - ss;
lt = del3 - st;

name = substr(ca,sn,ln);
street = substr(ca,ss,ls);
town = substr(ca,st,lt);

prline.text = name;
write file(a1) from(prline);

prline.text = street;
write file(a1) from(prline);
Sample Program 7 Contd …..
if verify(town,'.-ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789') ª= 0
then prline.text = '*****';
else prline.text = town;

write file(a1) from(prline);


end;
end;
end P14M0;
Sample Program 8
P16M0:proc P16M0 */
proc options(main);
dcl p16m2 entry;
dcl (number,pvalue) fixed decimal(5,2);
on eof bit(1) init('0'B);
on endfile(sysin) eof = '1'B;
do until(eof);
get list(number);
if ª eof then do;
call p16m2(number,pvalue);
put skip list(number,pvalue);
end;
end;
end;

P16M2:proc(number,pvalue);
dcl (number,pvalue) fixed decimal(5,2);
if number >= 100
then pvalue = number * 0.05;
else pvalue = number * 0.03;
end;

You might also like