You are on page 1of 169

PROGRAMMING

IN PL/I

1
INTRODUCTION

2
INTRODUCTION
• PL/I stands for Programming Language One.
• Introduced as General Purpose language in the age of
Specialty languages like COBOL, FORTRAN, ALGOL
etc..
• IBM combined features of ALGOL and COBOL in
designing PL/I.
• PL/1 is a high-level, procedural, modular, strongly
typed, block-structured language.

3
INTRODUCTION
contd....

• It is suitable for Business, Scientific and


Engineering Applications.
• Supported in
* IBM
* Honeywell/Bull
* DEC
* Data General
* CDC
* Burroughs

4
PROGRAM STRUCTURE

• PL/I programs consist of one or more procedures


• Each procedure is a block of code delimited by
statement PROCEDURE (or PROC) at the
beginning and END at the end.
• The highest level procedure is called MAIN.
eg: EXAMPLE: PROCEDURE
OPTIONS(MAIN);

5
PROGRAM STRUCTURE contd....

• Column 1 • Reserved for use of


operating system.
• May contain PL/1
• Column 2-72 statements or part of the
statement .
• Column 73-80 • may contain card
sequence number.

6
PROGRAM STRUCTURE
Contd....

• In our program, we will write as


SKILPRN : PROC OPTIONS(MAIN);
• SKILPRN is the statement label. Here it
names the Main Proc.
• Every PL/I statement ends with a ;
• The END can also include the PROC name.
END SKILPRN;
7
COMMENTS
•Comments are included within /* and */.
•Comments can be EMBEDDED in PL/I statements.
•For our program, we start as
/*******************************************/
/* Program to print SKILL Wise report extracting * /
/* data from Employee_Skill file */
/******************************************/

8
PL/I CHARACTER SET
• 60 Characters
* Extended alphabet of 29 characters : $, @, #, A - Z.
* 10 Decimal Digits : 0 - 9..
* 21 Special Characters : = + - * / ( ) , . ‘ % ; : & | > < _ ?
space.
* Special characters may be combined to create other
symbols such as >= etc.
* The symbol ** denotes exponentiation.
* The symbol || denotes concatenation
* The symbol -> denotes points to.

9
PL/I CHARACTER SET
Contd....

• As some of the special characters are not


printable and are not available in some
keyboards, a 48 character set is also defined that
uses: CAT GT GE LT LE NL NG NE NOT OR
AND PT
• # and @ are also not available in 48
character set.
* : is represented as .. and ; is represented as ,.

10
IDENTIFIERS

• Variable Names, Statement Labels, Procedure


Names, keywords, file names are identifiers.
• It can contain A - Z, @, #, $ , 0 - 9 and _.
• The first character of the identifier must be an
alphabetic character.

11
IDENTIFIERS
• Variable Names, Statement Labels, Internal
Procedure Names can be up to 31 characters.
• External Procedure Names can be maximum of
7 (8 for some compilers) characters.
• -, @, #, $ may not allowed for external
procedure names.

12
CONSTANTS
• They do not have a name, and remain unchanged.
• Decimal Fixed-Point Constants: 180, 3.1415,
+52.98, -100, 0.003
• Decimal Floating-Point Constants: 8.600000E+1,
-45E+11, .1E-7
• Character String Constants (Upto 256 Ch):
‘SHAKESPEARE'S’
• Bit String : ‘1’B, ‘1111010101’B, (64)’0’B

13
DATA TYPES
&
DATA MANIPULATION

14
DATA TYPES

1. DECIMAL FIXED POINT


2. DECIMAL FLOATING POINT
3. BINARY FIXED POINT
4. BINARY FLOATING POINT
5. CHARACTER STRING
6. BIT STRING

15
DECLARE STATEMENT

• DECLARE (or DCL) is used to


– Assign data types to variables.
– Define arrays and structures.
– Define files explicitly.
• May appear anywhere following PROC statement.
• NOT necessary for DECLARE to precede the
variable’s use.
• Normally, all DECLAREs are placed immediately
after PROC.
16
DATA ATTRIBUTES
• Base Attribute
– DECIMAL
– BINARY

• Scale Attribute
– FIXED means decimal point at a fixed point
– FLOAT means decimal point can change position

17
DATA ATTRIBUTES Contd....
• Mode Attribute
– REAL (default)
– COMPLEX. It contains Real and Imaginary portions.
Imaginary portion is followed by I.
Example : 46 + 2I
– DCL EX_1 DECIMAL FLOAT (5) COMPLEX;
• Precision Attribute
– Number of significant digits to be maintained.
– For FIXED scale, precision for decimal digits also specified.

18
DATA TYPES
• FIXED DECIMAL
– Packed Decimal Representation
– Default Precision : 5 Decimal Digits
– Maximum Precision : 15 Decimal Digits
– Example :
DCL GROSS_SALES FIXED DEC(7),
BASE_COMM FIXED DEC(9,2),
NET_SALES FIXED DEC; /*By Default 5 */

19
DATA TYPES Contd....

• FIXED BINARY
– Binary Representation
– Default Precision : 15 Bits
– Maximum Precision : 31 Bits
– Example :
DCL AVERAGE_SALARY FIXED BIN(15),
TOTAL_SALARY FIXED BIN(31,2);

20
DATA TYPES Contd....

• FLOAT DECIMAL
– Floating Point Decimal Representation
– Default Precision : 6 Decimal Digits
– Maximum Precision : 16 Decimal Digits
– Range of Exponent 10**-78 to 10**+75
– Example:
DCL MEAN_NUMBER FLOAT DEC(6),
VARIANCE FLOAT DEC(16);

21
DATA TYPES Contd....

• FLOAT BINARY
– Floating Point Binary Representation
– Default Precision : 21 Bits
– Maximum Precision : 53 Bits
– Range of Exponent : 2**-260 to 2**+252
– Example:
DCL NUMBER_1 FLOAT BIN(21)
NUMBER_2 FLOAT BIN(53);

22
DATA TYPES Contd....

• CHARACTER
– Character/Alphanumeric Representation
– No Default Length
– Maximum Length : 1000 for Constants, 32767
for Variables
– Example : DCL DESCRIPTN CHAR(20);

23
DATA TYPES Contd....

• BIT
– Bit Representation
– Logical Data
– No Default Length
– Maximum Length : 8000 for Constants, 32767
for Variables
– Example : DCL ITEM BIT(9);

24
PARTIAL DECLARATION OF
ATTRIBUTES
PL/I assigns default attributes to variable whose attributes
are not defined, or partially defined. Following table lists
them
Declared Attributes Default Attributes
DECIMAL FIXED (5,0)
DECIMAL FLOAT (6)
BINARY FIXED (15,0)
BINARY FLOAT (21)
DECIMAL FLOAT(6)
BINARY FLOAT(21)
FIXED DECIMAL(5,0)
FLOAT DECIMAL(6)
None - Initial Char I-N BINARY FIXED (15)
None - Initial Char Others DECIMAL FLOAT (6)
25
VARYING ATTRIBUTE
• Used to make a string (of character or BIT) of varying
length.
• It prevents padding of trailing unused bytes with spaces
of ‘0’ B.
• The length of the Variable is actual used Bytes
• The length is stored in the first two bytes
E.g: DCL EX_1 CHAR(30) VARYING;
DCL EX_2 BIT(10) VARYING;

26
DEFINED ATTRIBUTE
• Equates two or more variables to point to same area.
• Example :
DCL A CHAR(10),
B CHAR(5) DEF A,
C CHAR(8) DEF A;
• A is called Base Variable, B and C are Defined
Variables.
• A Based Variable cannot have DEF attribute

27
DEFINED ATTRIBUTE
Contd....

• Invalid Example :
DCL A CHAR(10),
B CHAR(5) DEF A,
C CHAR(8) DEF B;
• POSITION attribute is used to overlay at any
position of a variable.
• Example :
DCL AA CHAR(40),
BB CHAR(10) DEF AA POSITION(21);
BB then refers to byte positions 21 to 30 of AA.
28
INITIAL ATTRIBUTE
• Used to assign value to variables to variables at Declare.
Example:
DCL NET_SALES FIXED DEC(7,2) INIT(0,0),
MAXIM FIXED BIN(31) INIT(2147483647),
MILE FLOAT DEC INIT(5280),
BETA FLOAT BIN(53) INIT(10011E+72B),
PATTERN BIT(16) INIT((8)’10’B),
TITLE CHAR(11) INIT(‘EXAMPLE END’);

29
ARITHMETIC OPERATORS
• **, *, /, + , - .
• Example :
X ** (J*2)
• Scope of declaration OF Identifiers
P1:PROC;
DCL A,B;
P2:PROC;
DCL C,D;
Y = C*D;
END P2;
END P1;
30
LOGICAL TESTING

31
LOGICAL TESTING

✦ IF and SELECT statements is used for logical testing.

✦ Operators used are :


GE or >=
GT or >
NE or ~=
LT or <
LE or <=
NL or ~<
NG or ~>
= (FOR EQUAL)
32
LOGICAL TESTING Contd....
• Simple IF:
IF A=B THEN X = 1;

• Complex IF:
IF A=B THEN X=1;
ELSE X=2;

* When more than one statement is required for the condition,

DO group is used.
IF A=B THEN
DO;
X=1;
Y=2;
END;
33
LOGICAL TESTING Contd....

• Example of NESTED IF condition

IF A = B THEN
IF A = C THEN
X = 1;
ELSE
X=2;
ELSE
X = 3;

34
LOGICAL TESTING Contd....

• Example of NULL action for a condition

IF A = B THEN
IF A = C THEN
X = 1;
ELSE; /* NULL ELSE */
ELSE
X = 3;

35
THE CASE OPTION

✦ SELECT statement implements the CASE structure.

• General Format

SELECT (optional expression);


WHEN(expression1) action 1;
WHEN(expression2) action 2;
....
OTHERWISE actionN;
END;

36
THE CASE OPTION
Contd....
EXAMPLE 1:
SELECT (SHIP_CODE);
WHEN (110) CALL SHIP_BEST_WAY;
WHEN (120) CALL SHIP_UPS_REGULAR;
WHEN (130) CALL SHIP_UPS_BLUE_LABLE;
WHEN (140) CALL SHIP_UPS_RED_LABEL;
OTHERWISE CALL SHIP_CODE_ERROR;
END;

EXAMPLE 2:
SELECT;
WHEN (BALANCE<0) CALL NGTV_BAL_RT;
WHEN (BALANCE=0) CALL ZERO_BAL_RT;
OTHERWISE CALL PSTV_BAL_RT;
END;
37
LOGICAL OPERATORS
• The Conditional Expression can be combinations of
individual conditions joined using Logical Operators.
• The three logical operators allowed in PL/I are
~ or NOT, & or AND, | or OR
• These can also be used in assignment
Example A = B > C & D < E.
A is set to 1 when the expression is true, 0 otherwise.

38
DO LOOPS

39
DO LOOPS

✦ Three types of DO LOOPS:

✦ITERATIVE DO.
✦WHILE DO.
✦ UNTIL DO.

40
DO LOOPS Contd....

✦ITERATIVE DO
* General Format
DO I = Exp1 TO Exp2 BY Exp3;
.....
END;
* BY Exp3 is optional. If omitted, BY 1 is assumed.
Example DO I = 60 TO 1 BY -1;
DO OUTCOME = ‘WIN’,’DRAW’;
* A Variation allowed is DO I = 1 TO 5,7,9,13 TO 20;
Example DO WHILE(-ENDOFFL);

41
DO LOOPS Contd....

✦ WHILE DO
*General Format
DO WHILE(Condition);
....
END;
*The statements are repeated till the Condition is true.
*The condition is tested before going into the loop.
Example DO WHILE(EN-OF-FL);

42
DO LOOPS Contd....
✦ UNTIL DO
* General Format
DO UNTIL(Condition);
....
END;
* The statements are repeated till the condition is false.
* The condition is tested after executing the statements in the loop.
Example DO UNTIL(SUB = ARRAY_SIZE);

• The UNTIL and the WHILE options can also be used in the
iterative Dos
Example DO K = 1 TO 10 WHILE (X > 100);
DO I = 1 TO 8 UNTIL (A = B);

43
DO LOOPS
Contd....
* DO WHILE (CTR < 50);
....
END;

* DO UNTIL (EOF);
....
END;

* J=10;K=2;
DO I=1 TO J BY K;
....
END;

44
DO LOOPS Contd....
* DO K=60 TO 1 BY -1;
....
END;

* DO K = 1 TO 5,8 TO 18,40 TO 44;


END;

* DO I = 1 TO 10,21 TO 30 WHILE (A=B);


END;

* DO I=1 TO 99;
DO J=1 TO 100;
....
....
END;
END;
45
BUILT-IN FUNCTIONS

46
BUILT-IN FUNCTIONS

These are some BUILTIN in functions.

1. Arithmetic
2. Mathematical
3. Array Handling
4. String Handling
5. Condition Handling
6. Storage Control
7. Other functions

DECLARE DATA BUILTIN;

47
BUILT-IN FUNCTIONS

These are some Arithmetic Functions.

1. SQRT Y=SQRT(X);
2. ABS Y=ABS(X);
3. MIN Y=MIN(X);
4. MAX Y=MAX(X);
5. CEIL Y=CEIL(X);
6. FLOOR Y=FLOOR(X);
7. TRUNC Y=TRUNC(X);
8. SIGN Y=SIGN(X);
9. ROUND Y=ROUND(M,N);
10. MOD Y= MOD(X);

48
BUILT-IN FUNCTIONS

These are some Mathematical functions

1. SIN Y=SIN(X);
2. COS Y=COS(X);
3. TAN Y=TAN(X);
4. LOG Y=LOG(X);
5. EXP Y=EXP(X);

49
BUILT-IN FUNCTIONS

These are some String Handling functions

1. CHAR Y=CHAR(X);
2. BIT Y=BIT(X);
3. INDEX Y=INDEX(X);
4. LENGTH Y=LENGTH(X);
5. REPEAT Y=REPEAT(X,Y);
6. SUBSTRN Y=SUBSTR(X,Y,Z);
7. TRANSLATE Y=TRANSLATE(X,Y,Z);
8. VERIFY Y=VERIFY(X,Y);

50
STRING HANDLING FUNCTIONS

* CHAR
DCL A FIXED BIN(31) INIT (175);
C=CHAR(A); /*C=‘ 175’*/
CHAR (X,N);

* BIT
DCL NUMBER FIXED BIN(31);
ITSY_BITS = BIT(NUMBER);

* INDEX
DCL NAME CHAR(30) VARYING;
NAME = ‘ABRAKADABRA’;
POS=INDEX(NAME,’DA’); /*POS=7*/ 51
STRING HANDLING
FUNCTIONS Contd....

* LENGTH
NAME = ‘ABRAKADABRA’;
LTH=LENGTH(NAME); /*LTH=11*/

* SUBSTR
X=‘PL/1LANGUAGE’;
Y=SUBSTR(X,1,4); /* Y=‘PL/1’ */

* VERIFY
DCL TV CHAR(5) INIT(‘123.4’);
DCL ND CHAR(5) INIT(‘12345’);
DCL XY FIXED BINARY;
XY= VERIFY(TV,ND); /* XY=4*/
IF THERE IS NO DIFFERENT CHARACTER RETURNS 0. 52
STRING HANDLING
FUNCTIONS Contd....

* REPEAT
DCL NAME CHAR(12);
NAME=(2)’RANGA’; /*RANGA RANGA*/
NAME=REPEAT(‘RANGA’,2);

* TRANSLATE
TRANSLATES(S,R,P);
S--> SOURCE STRING
R-->REPLACEMENT STRING
P -->POSITION STRING

53
DATE AND TIME FUNCTIONS

* Returns Current Date and Time


eg: CURRENT_DATE = DATE;
CURRENT_TIME = TIME;

* Format
DATE YYMMDD
TIME HHMMSSIII

* Explicitly declared with BUILTIN


DCL(DATE,TIME) BUILTIN;

54
DATA CONVERSIONS

• Arithmetic and Logical operations can not be done


when operators are of different types.
• An automatic data conversion is carried out in such
case.
• If base of the operators differ, DECIMAL is converted
to BINARY.
• If scale of the operators differ, FIXED is converted to
FLOAT.
• If both CHARCTER and BIT are present, BIT is
converted to CHARCTER, each bit becomes a
character.
55
DATA CONVERSIONS

• DCL A FIXED DEC, B FLOAT DEC;

• DCL C FIXED BIN, D FLOAT BIN;

• DCL K CHAR(13), I CHAR(5), J BIT(8);

• Y=A + B; A IS CONVERTED TO FLOAT DEC


• I= A* C; A IS CONVERTED TO FIXED BIN
• Z= A/D; A IS CONVERTED TO FLOAT BIN;
• K= I || J; J IS CONVERTED TO CHAR(8)

56
PROCEDURES
IN
PL/1

57
PROCEDURES IN PL/1

There are 3 types of procedures.

1. MAIN Procedures
2. SUBROUTINE Procedures
3. FUNCTION Procedures

58
PROCEDURES IN PL/I Contd....

• ADVENTAGES
– Save coding effort
– Improve program readability
– Facilitates program maintenance
– Reduces total programming time

59
PROCEDURES IN PL/I Contd....

• MAIN procedure.
* The highest level procedure is MAIN.
• SUBROUTINE procedure .
* Invoked by a CALL from another procedure.
CALL SKILLOC(SKILL_ARRAY,SKILL_CODE);
* The PROC statement must also include the
parameters.
SKILLLOC : PROC (SK_ARRAY, SK_CODE);
* The parameters include both input and output
arguments.
60
PROCEDURES IN PL/I
Contd....
• FUNCTION procedure.
* Invoked by a reference to the function with parameters.
PRCNTG_EXP(I) = EXP_PER(TOTAL_YRS(I))
* Returns a single value as result of the function. Must
contain at least one RETURN statement.
* The PROC statement also include RETURNS clause.
* RETURNS clause specifies the type of the result.
EXP_PER : PROC(A) RETURNS(FIXED DECIMAL(5,2));
RETURN((A*100)/ORGNZN_EXP);
END EXP_PER;

61
TYPES OF SUBROUTINE
PROCEDURES

• External
* Procedures written in a separate dataset/member.
* They are compiled separately.
* An external procedure should be declared in the calling
procedure using ENTRY option.
DCL SUBRTN ENTRY;

• Internal
* Defined within the PROC and END of another PROC.

62
SUBROUTINES PROCEDURES
• A SUBROUTINE Procedure is invoked by a CALL.
CALL SUMPROC(A,B,C);
SUMPROC -> SUBROUTINE Name
A,B,C -> ARGUMENTS List
• PARAMETERS must be declared in a parenthesized
list in the procedure statement of invoked procedure.
For Example:
SUMPROC:PROC(X,Y,Z);
x,y,z -> PARAMETER List
CALL VALID(NAME,ADDRESS,ERROR);

63
FUNCTION PROCEDURES
• A FUNCTION is procedure that returns a single value
to the invoking procedure.
Z = CALC(X,Y);
CALC -> A FUNCTION Procedure Name
X,Y -> ARGUMENT List
Result is assigned to Z.
For Example,
IF CALC(X,Y) < 0 THEN
CALL NEGATIVE_RT;
CALC:PROC(A,B,C) RETURNS (FIXED DEC(7));
RETURN (A+B+C);
END CALC;
64
FUNCTION PROCEDURES
Contd....

• RECURSIVE Procedures
I = 4;
RESULT = N_FACTORIAL(I);

N_FACTORIAL:PROC(N);
K = 1;
DO I = 1 TO N;
K = K*1;
END;
RETURN(K);
END N_FACTORIAL;
65
FUNCTION PROCEDURES
Contd....

• RECURSIVE Procedures
I = 4;
RESULT = N_FACTORIAL(I);

N_FACTORIAL:PROC(N) RECURSIVE;
K = N-1;
IF K=1 THEN
I = N;
ELSE
I=N*N_FACTORIAL(K);
RETURN(I);
END N_FACTORIAL; 66
CONDIONS
AND
ON-STATEMENT

67
ON STATEMENT

• ON statement is used to specify action to be taken


when any subsequent occurence of a specified
condition causes a program interrupt.
• It prevents the system from causing the program
interrupt.

68
ON STATEMENT
Contd....

• The general format of ON statement


ON condition
BEGIN;
actions;
END;
If the action is only one statement, the BEGIN & END
may be ignored.
• The condition can be simulated using SIGNAL
statement. The format is SIGNAL condition;

69
ON STATEMENT
* Types of conditions
* I/O conditions
- ENDFILE(file name)
- ENDPAGE (file name)
- RECORD (file name)
- TRANSMIT (file name)
- CONVERSION
- SIZE
* Arithmetic conditions
- CONVERSION
- FIXEDOVERFLOW
- OVERFLOW
- UNDERFLOW
- ZERODIVIDE
- SIZE
* Types of Actions
* System Action
* Program specified Action
* Null Action (e.g: ON ENDPAGE(PRINTR);)

70
ON STATEMENT
Contd....
These are Built in functions of on-units
• ONCODE function returns Interrupt Type.
• ONLOC function returns Entry point of interrupted PROC.
• ONCHAR function returns character causing CONVERSION
interrupt.
• ONSOURCE returns source field in CONVERSION interrupt.
• ONKEY function returns the value of the key that caused the I/O
condition.
• ONFILE function returns the file which causes the I/O condition.

Syntax: ON condition on-unit;

71
ON STATEMENT Contd....

I/O conditions
• ENDFILE(file name)
• ENDPAGE(file name) when line count per pagecrosses
• RECORD(file name)
• TRANSMIT(file name) Raises when I/O device tansmit
• CONVERSION invalid conversion attempted
• SIZE

72
DATA CONVERSIONS Contd....

• ON ENDFILE
DCL MORE_RECORDS BIT(1) INIT(‘1’B);
DCL YES BIT(1) INIT(‘1’B’);
DCL NO BIT(1) INIT(‘0’B);
ON ENDFILE(SYSIN)
MORE_RECORDS=NO;
DO WHILE(MORE_RECORDS);
GET LIST(X,Y,Z);
PUT LIST(X,Y,Z);
END;

73
ON STATEMENT
Contd....
• ON KEY occurs when
– Keyed record cannot be found for a READ or REWRITE.
– Attempt is made to WRITE duplicate key record.
– No space is available to add the new keyed record.
• ON UNDEFINED FILE is caused by
– Conflicting attributes.
– Incomplete attributes.
• ON RECORD occurs when
– Max size of record is greater than the variable (for F, V and U).
– Size of record is less than the size of variable (for F)

74
CONVERSIONS
Contd.…
• SIZE CONDITON
DCL V1 FIXED DEC(4);
DCL V2 FIXED DEC(5) INIT (12345);
V1=V2;
• CONVERSION CONDITON
DCL X BIT (4);
X = ‘10A1’;
• SIGNAL Condition
ON ERROR BEGIN;
ON ERROR SYSTEM;
PUT DATA; END;

75
BUILT IN FUNCTIONS
FOR ON UNITS

ON ERROR
PUT LIST(ON CODE);
ONCODE
ONCHAR
CHAR = ONCHAR;
ON SOURCE
SOURCE = ONSOURCE;

76
PROGRAM CHECKOUT
CONDITIONS
• STRING RANGE CONDITION
DECLARE NAME CHAR(20);
DECLARE LAST CHAR(16);
LAST = SUBSTR(NAME,5,20);

• STRINGSIZE CONDITION
DCL REC CHAR(5); DCL SEN CHAR(12);
REC = SEN ;

77
ON STATEMENT Contd....

ARITHMETIC conditions
• CONVERSION IN ASSIGNMENT
• FIXEDOVERFLOW /* C= A*B */
• OVERFLOW /* C= A*B */
• UNDERFLOW /* C= A*B */
• ZERODIVIDE /* C=A/B */

78
ON STATEMENT Contd....

• CONDITIONS are Enabled and disabled trough prefix NO.

• The prefix NO is used for disable a condition

E.g: (NO OVERFLOW):PROG1:PROC

79
ARRAY’S AND STRUCTURES

80
ARRAYS
✦ An array is a table of data items in which each
item has the same attribute as every other item in
the array.
DECLARE TEMPS(365) FIXED DEC (4,1);
THE EXTENT IS 365.
DCL TABLE(0:11) FIXED DEC(5);
EXTENT IS 12.
DCL LIST(-2:6) FIXED BIN(15,0)
INIT(1,20,82,11,7,38,38,48,28);
SUBSCRIPT VALUES ARE (-2,-1,0,1,2,3,4,5,6)

81
ARRAYS Contd....
A 2-DIMENSIONAL TABLE CAN BE DECLARED BY:
DCL TABLE(6,2) FIXED DEC(5);
DCL AXIS(-3:3,-4:4) FLOAT DEC(6) INIT ((63)0);
DCL POP (2:50,10) FLOAT DEC(6);

WE CAN HAVE
✦VARIABLE SUBSCRIPTS
✦SUBSCRIPT EXPRESSIONS

Example: T = TEMPERATURES(K-1+Q);

SUBSCRIPTED SUBSCRIPTS
Example: Z = X(Y(I));
82
ARRAYS Contd....
✦ ARRAY MANIPULATION BUILT IN FUNCTIONS
1. DIM 5. PROD
2. LBOUND 6. POLY
3. HBOUND 7. ANY
4. SUM 8. ALL

✦ I/O OPERATIONS AND ARRAYS


DCL AMT(5,1) FIXED DEC(3);
GET LIST(AMT);
Data items are read into
AMT(1,1), AMT(1,2) , AMT(1,3)
AMT(1,4) .............. AMT(5,4) 83
ARRAYS Contd....
✦ REPETITIVE SPECIFICATION
GET LIST((TEMPS(I) DO I=1 TO NUM));
GET LIST(((A(I,J)DO I=1 TO 5)DO J=3 TO 7));
OR DO J=3 TO 7;
DO I=1 TO 5;
GET LIST(A(I,J)); END; END;

DCL A(50) FIXED INITIAL (0);


FIRST ELEMENT WILL HAVE 0.
DCL A(50) FIXED INITIAL ((50)-1);
DCL TAB(10) CHAR(2) INIT ((2) (2)’A’);
DCL TAB(10) CHAR(5) INIT ((10) (1)’EMPTY’)
DCL A(3) FLOAT DEC(6) INIT(10,*,30);
84
ARRAYS Contd....
✦ ARRAY ASSIGNMENT
TWO TYPES OF MOVE OPERATIONS

* SCALAR-TO-ARRAY
Entire array is assigned a scalar value.
DCL MONTHS(12) FIXED DEC(4,1); MONTHS = 0;

* ARRAY-TO-ARRAY
The array may be moved to another array.
DCL A(5,5) FLOAT DEC(6);
DCL B(5,5) FLOAT DEC(6);
A = B;
/ * ARRAY TO ARRAY */ 85
ARRAYS Contd....

✦ PREFIX, OPERATORS AND ARRAYS


A= 1 3 -5
4 -2 -7
6 12 13

A = -A WOULD CHANGE ARRAY TO


-1 -3 5
-4 2 7
-6 -12 -13

86
ARRAYS Contd....
✦ INFIX OPERATORS AND ARRAYS
A = 5 10
20 25

A*5 = 25 50
100 125
✦ CROSS SECTIONS OF ARRAYS
TOTAL = PERCENT * (*,4) * PRICE

87
STRUCTURES
✦ STRUCTURE.

* A Structure is a collection of data items requiring


storage for each item to be a particular order and
having a logical relationship to one another.
* Enables related data or different attributes to be held and
referenced together.

* Level number :- Major structure name will have level


number 1. Each level will be given a greater number .
* Maximum level number is 255.
* The major level number must be declared first.

88
STRUCTURES
Example
DCL 1 SALARY RECORD
2 NAME,
3 LAST,
3 FIRST,
3 MIDDLE,
2 MAN_NUMBER,
2 HOURS,
3 REGULAR,
3 OVERTIME,
2 WAGES,
3 REG_PAY,
3 OT_PAY;

89
STRUCTURE ASSIGNMENT
DCL 1 INPUT_REC,
2 F1
2 F2;
DCL 1 DISK_REC,
2 D1,
2 D1;
DISK_REC = INPUT_REC;
DCL 1 INPUT,
2 EMP_NAME CHAR(20),
2 EMP_NUM CHAR(6);
DCL 1 OUTPUT,
2 CARRIAGE_CNTL CHAR(1),
2 EMP_NUM CHAR(12),
2 EMP_NAME CHAR(25);
OUTPUT = INPUT, BY NAME;
✦ SCALAR-TO-STRUCTURE
INPUT = ‘ ‘; 90
OVERLAY DEFINING
DCL 1 CUSTOMER
2 NAME CHAR(20)’
2 QTY FIXED DEC(5);
DCL CUST CHAR(25) DEFINED CUSTOMER;

✦ STRUCTURE IN STREAM I/O

GET EDIT(CUSTOMER) (A(20), F(5));


PUT EDIT(NAME,QTY) (A(20),F(5));

91
DATA AGGREGATES Contd....

✦ ARRAY
* Arrays can have upto 15 dimensions.
* Extent of each dimension begins at 1 by default.
* DCL A(75) CHAR(20); defines an array of 75
elements.
* DCL B(-1:10) FIXED BIN(15); defines an array of 12
elements

92
ARRAYS IN STRUCTURES
DCL 1 INVENTORY_ITEM,
2 PART_NO CHAR(8),
2 QTY PIC ‘9999’,
2 SALES_HIST(12) PIC ‘99999’;
✦ ARRAYS OF STRUCTURES
DCL 1 WEATHER(12)
2 TEMP,
3 HIGH,
3 LOW,
2 VELOCITY;
✦ LIKE ATTRIBUTE
DCL 1 BUDGET,
2 RENT FIXED DEC(5,2);
2 TRANS FIXED DEC(7,2);
2 FOOD FIXED DEC(5,2);
DCL 1 COST_OF_LIVING LIKE BUDGET;
93
DATA AGGREGATES

Contd....
✦ ARRAY OF STRUCTURE

Example:
DCL 1 EMP_WORK_DTLS,
3 EMP_NUM CHAR(5),
3 WEE_BEGIN CHAR(6),
3 ATTNDNCE(7),
5 IN_TIME FIXED DEC(5,2),
5 OUT_TIME FIXED DEC(5,2);

94
DATA AGGREGATES Contd....
✦ Initializing Arrays

* DCL A(50) FIXED INIT(0); will put 0 to first element.


* DCL A(50) FIXED INIT((50)0); will initialize the whole
array.
* DCL B(9,9) INIT((81)-1) will put -1 in all the elements.

95
STORAGE CLASSES

96
STORAGE CLASSES

4 PL/1 STORAGE CLASSES ARE

* AUTOMATIC
* STATIC
* BASED
* CONTROLLED

97
STORAGE CLASSES Contd....
✦ AUTOMATIC variables are accesable within the defining
PROC and all its INTERNAL PROCs.

✦ STATIC variables are retained (with their values) between


different invocation of the same procedure.

✦ BASED variables are pointed to by a POINTER.

✦ CONTROLLED variables are provided memory only


through ALLOCATE statement and are removed from
memory through FREE statement.

98
Automatic storage
✦ AUTOMATIC

MAIN: PROC OPTIONS(MAIN)


DCL 1 STRUCT1,
2 A,B,C FIXED(5,2),
2 FIXED(5);
...... CALL P1;
CALL P2;
P1 :PROC;
DCL TABLE(100) CHAR(10); ...........
END P1;
P2 :PROC;
DCL LIST(500) FIXED; .................
END P2;
END MAIN;
99
Automatic storage Contd....
The Program In Main Storage has
MAIN
P1
P2
STRUCT1 ............... AUTOMATIC
TABLE ............... AUTOMATIC
There is no separate storage for TABLE and LIST data names.

• PROLOGUE
The allocation of dynamic storage for variables having AUTOMATIC
attribute is performed by this routine.

• EPILOGUE
The release of main storage allocated for AUTOMATIC variables is
done by this. 100
Static storage
✦ STATIC STORAGE

DCL 1 STRUCT1 STATIC,


2 A FIXED DEC(5,2),
2 B FIXED DEC(5,2),
2 C FIXED DEC(5,2),
2 D CHAR(10);
DCL TABLE(100) CHAR(10) STATIC;
DCL LIST (500) STATIC FIXED DEC(5);

There is separate storage for TABLE and LIST date names.

101
Based storage

✦ BASED STORAGE
DCL P POINTER;
DCL A(100) FIXED DEC(5) BASED(P);
P CONTAINS THE ADDRESS OF ARRAY.
Examples of using pointer are
* DCL (P,Q) POINTER;
P = ADDR(AREA);
Q = P;
* DCL VALUE1 BIT(32) BASED (P);
DCL VALUE2 FLOAT(6);
P = ADDR(VALUE2);
* READ FILE(INPUT) SET (P);
LOCATE FILE(OUTPUT) SET (Q);

102
POINTER AND BASED
• Example :
DCL VAR1 CHAR(6);
DCL BSD1 PIC ‘6(9) BASED (P);
DCL P POINTER;
DCL ADDR BUILTIN;/*PL/I SUPPLIED*/
• Statement P = ADDR(VAR1); will assign address
of VAR1 to P.
• As BSD1 is BASED on P, its space will be where P
points.
• Thus BSD1 and VAR1 points to same location.

103
Controlled storage

✦ CONTROLLED STORAGE

Variables that are declared with the Controlled are


allocated storage only upon the execution of PL/1
statement : ALLOCATE. It will be freed after the
execution of : FREE PL/1 statement only.

104
Controlled storage Contd....
✦ CONTROLLED STORAGE

PGM: PROC OPTIONS (MAIN);


DCL A(600,400) CONTROLLED,
B(600,400) CONTROLLED,
(A1,B1)(600,400) CONTROLLED;
ALLOCATE A,A1;
GET LIST(A);
DO I=1 TO 300;
DO J=1 TO 300;
A1(I,J) = A(I.J);
END;
END;
FREE A;
PUT DATA (A1);

105
Controlled storage Contd....
ALLOCATE B,B1;
GET LIST(B);
DO I=1 TO 300;
DO J=1 TO 300;
B1(I,J) = B(I.J);
END;
END;
FREE B;
PUT SKIP DATA (B1);
END PGM;

106
Controlled storage Contd....
✦ STACKING CONTROLLED VARIABLES

If a CONTROLLED variables is allocated again and again


before allocation has been freed , the allocation is not lost.
Instead it is saved for the feature use but is not available to
programmer until the second allocation has been freed.

Example: DCL A(4) FLOAT CONTROLLED;


ALLOCATE A;
A=100;
ALLOCATE A;
A=200;
PUT SKIP LIST(A);
FREE A;
PUT SKIP LIST(A); 107
FILE PROCESING
IN
PL/1

108
FILE DECLARATION
• DECLARE (PAYROLL) FILE (Other Attributes)
* Other Attributes include:
– Type of Transmission (Stream or Record)
– Direction of Transmission (Input, Output or Update)
– Physical Environment.
• The program logic includes
1. Define the File
DCL PAYROLL FILE;
2. Open the File
OPEN FILE (PAYROLL);
3. Process information in the File
READ, WRITE OR REWRITE OR GET, PUT
4. Close the File.
109
CLOSE FILE(PAYROLL);
FILE DECLARATION
• File Declaration - Format
DCL filename INPUT/OUTPUT STREAM/RECORD
PRINT ENV(recfm BLKSIZE(blocksize))
• Underline means default. Italics indicates
optional.
• PRINT option needs to be used when the file is for
printer and contains carriage control characters.
• ENV option specifies characteristics of the file. It
is better not to mention this in the program, but
code in the JCL.
110
OPEN STATEMENT
• Multiple files can be opened in one statement, each
FILE attribute should be delimited by a comma or
semicolon (for the last).
OPEN FILE(INFILE),FILE(OUTFL);
• For PRINT files, optionally declare
OPEN FILE(PRNFL) PAGESIZE(50) LINESIZE(133);
• File Names (7 OR 8 CHARS. MAX)
For Example,
OPEN FILE(PRINTR) PAGESIZE(50);
OPEN FILE(PRINTR) LINESIZE(121);
OPEN FILE(DATAIN) TITLE(FNAME);
111
CLOSE STATEMENT

• For CLOSE, only FILE parameter is specified.


CLOSE FILE(filenm);
For Example,
OPEN FILE(WORK) OUTPUT;
/* Write Records */
CLOSE FILE(WORK);
OPEN FILE(WORK) INPUT;
/* Read Records */

112
SYSIN AND SYSPRINT

• SYSIN is the default for the standard input file and


SYSPRINT is for the standard output file.
GET LIST(A,B,C) is equivalent to
GET FILE(SYSIN) LIST(A,B,C);
PUT LIST(A,B,C) is equivalent to
PUT FILE(SYSPRINT) LIST(A,B,C);
• List -Directed I/O statements, where a file name is
explicitly codes take this general form.
GET FILE(FILE NAME) LIST(DATA NAMES)
PUT FILE(FILE NAME) LIST(DATA NAMES)
113
FILE PROCESSING

• Two types of file processing:


* STREAM I/O : Data is considered as a continuous
stream of data items, record and block boundaries are
ignored. Only sequential files can be accessed.
* RECORD I/O : Data is considered as collection of
records.

114
STREAM I/O
• LIST Directed
– For Input
• Format GET FILE(filenm) LIST (data list);
• File attribute is optional. If omitted, SYSIN is assumed.
• There should be atleast one blank between two data items.
• The Character data should be included within quotes.
– For Output
• Format PUT FILE(filenm) LIST(data list);
• FILE attribute is optional. If omitted, SYSPRINT is
assumed.
• Each data item is printed at the next TAB position. Tab
positions are 1, 25, 49, 73, 97, 121.
115
STREAM I/O
Contd....
• EDIT Directed
– For Input
• Format GET FILE(filenm) EDIT(data list) (format list);
• FILE attribute is optional. If omitted, SYSIN is assumed.
• Format List specifies format and length in which each data item is
to be read.
Example :
GET EDIT (EMP#,NAME,RATE,HOURS,DEDUCTIONS)
(COLUMN(1),A(6),A(20),F(4,2),F(3,1),F(5,2));
And data to be specified
435267PANDURANG SHIVALKAR 105541310321

116
STREAM I/O
Contd....
• EDIT Directed Contd....
– For Input (Contd....
• If same format is to be used at various places in the program,
use remote format option.
• Example :
GET EDIT(EMP#,NAME,RATE,HOURS,DEDUCTIONS)
(R(FMT));
.....
FMT :
FORMAT(COLUMN(1),A(6),A(20),F(4,2),F(3,1),F(5,2));
– For Output
• Format PUT FILE(filenm) LIST(data List) (format list):
• FILE attribute is optional. If omitted, SYSPRINT is assumed

117
STREAM I/O Contd....

• DATA Directed
– For Input
• Format GET FILE(filenm) DATA(data list);
• FILE attribute is optional. If omitted, SYSIN is assumed.
• The data should be specified as identifier = value.
• Example :
GET DATA(A,B,C,D);
And data to be specified is
A = 12.3, B = 57, C=‘ABCDEFGH’,D=‘1110’B;

118
STREAM I/O Contd....
• DATA Directed Contd....
– For Output
• Format PUT FILE(filenm) DATA (data list);
• FILE attribute is optional. If omitted, SYSPRINT is assumed.
• Example : PUT DATA(A,B,C);
Will print as
A = 12.3 B= 57 C = ‘ABCDEFGH’;
– COUNT function
• Returns the number of data items transferred during a GET or PUT.
• Format I = COUNT(filenm);
• File filenm must have been opened in STREAM option

119
RECORD I/O
• The whole data structure is transferred as a block.
• Thus a STRUCTURE is usually associated with record I/O.
• Keywords READ and WRITE specify Input and Output
operations.
• Typical READ Example: READ(filenm1) INTO(data area);
• Typical WRITE Example: WRITE(filenm2) FROM(data area);
• filenm1 and filenm2 should have RECORD attribute.
DCL filenm1 FILE INPUT RECORD ENV(F RECSIZE(80));
DCL filenm2 FILE OUTPUT RECORD ENV(F RECSIZE(80));
DCL data area CHAR(80);

120
RECORD - I/O Contd....
• CARRIAGE CONTROL IN RECORD I/O
– Two different sets of carriage-control characters are
• CTLASA
• CTL360
DCL PRINTR FILE OUTPUT RECORD ENV(F RECSIZE(81) CTLASA);
DCL PRN_AREA CHAR(81);
PRN_AREA = ‘1’ || DATA_AREA;
WRITE FILE(PRINTR) FROM PRINT_AREA;

121
EDITED IDENTIFIER
• Mainly used for printing. Data stored in zoned
decimal format.
• PICTURE (or PIC) is used for defining edited
identifier.
• Example :
DCL A PIC ‘999’; /* Three Digit Number */
DCL B PIC ‘(3)9.99’ /* 5 Digits, 2 After Dec.*/

122
PICTURE CHARACTERS
• 9 indicates any decimal digit
• V indicates the assumed location of a decimal point.
Example :
999v99 FIXED(5,2) 123.45 123^45 5
V99999 FIXED(5,5) 12345 ^00000 5
9V9 FIXED(2,1) 123.45 3^4 2
999V99 FIXED(5,2) -123.45 123.45 5

123
PICTURE CHARACTERS
Contd....
• The picture characters S,+,- may appear to the left or
right of all digit positions.
Example :
S999V99 FIXED(5,2) -123.45 -123^45 6
S999V99 FIXED(5,2) +123.45 +123^45 6
DCL SUM PIC ‘9999’;
DCL A PIC ‘999’;
DCL B PIC ‘999’;
SUM = A + B;
Z PICTURE CHARACTER - Zero Suppression
DCL TABLE(20) CHAR(4);
READ FILE(DATA)INTO (TABLE);
124
CHARACTER - STRING PICTURES
DCL A PICTURE `(5)9V99;
DCL B PICTURE `(5)9V99;
DCL A PIC`SZZZ9’;
DCL BIG_VALUE PIC`999,999V.99’
INIT(104056.98); /* 104,056.98 */
• The Blank(B)
DCL A PIC ‘999V99BBB’;
• The Slash(/)
DCL EDITED_RUN_RATE PIC ‘Z9/99/99’;
• The Dollar Sign($)
DCL B PIC ‘$$$$V.99’ INIT (12.34);

125
CHARACTER - STRING PICTURES
Contd....
• The Asterisk (*)
DCL AMT_PAID PIC ‘*****9V.99’
INIT(101.75);
• Credit and Debit Characters (CR and DB)
DCL D PIC ‘99V.99CR’ INIT(-12.34); 12.34CR
Both DB and CR will appear in the edited field of negative values
• A A through Z or a blank
• X Any character
• 9 Only 0 through 9
• P‘PICTURE SPECIFICATION’
PUT EDIT(ASSETS)(P’$$$$,$$$V.99’);
Contains any character allowed in the picture declaration.
126
RECORDS - STRUCTURES

DCL 1 NAME_ADDR,
2 NAME CHAR(20),
2 STREET CHAR(25),
2 CITY CHAR(25),
2 REST CHAR(5);
DCL 1 PRINT_AREA
2 CARRIAGE_CONTROL CHAR(1),
2 PRINT_NAME CHAR(20),
2 UNUSED_1 CHAR(5) INIT(‘ ‘),
2 PRINT_STREET CHAR(25),
2 UNUSED_2 CHAR(5) INIT(‘ ‘),
2 PRINT_CITY CHAR(25);
127
CHARACTER - STRING PICTURE
STRUCTURES
DCL 1 SALARY_RECORD,
2 NAME,
3 LAST CHAR(1) INIT (`XYZ’),
3 FIRST CHAR (8) INIT (`ABCD’),
3 MIDDLE CHAR(2) INIT(`PQ’),
2 SALARY PIC ‘9999V99’
INIT (9898.90);
We can use the elementary items :
e.g., NAME.LAST
128
FILE ORGANIZATIONS

• CONSECUTIVE
– Records are placed in physical sequence of writing.
– Records are also retrieved in same order.
– Inserting new records to file means creating a new
version.
– Record can be altered in UPDATE mode.

129
FILE ORGANIZATIONS
Contd....
• INDEXED
– Records are arranged in sequence of the specified key.
– Records can be accessed randomly for a key value.
– Records can be inserted and deleted from indexed files
– Deleted records are flagged (8)‘1’B in the first Byte. Normally
the first byte in the record is defined as the delete flag.
– Sequential processing ignores the deleted records, but random
read returns this record. The program accessing the file
randomly should check for delete flag.

130
FILE ORGANIZATIONS
Contd....
• REGIONAL
– Datasets are divided into regions.
– REGIONAL(1) means each region contains one
unblocked fixed format record without any key.
– REGIONAL(2) means each region contains one
unblocked fixed format record that is identified by a
key preceding the record.
– REGIONAL(3) means each region is a track containing
unblocked record(s) each preceded by a key.
– Records are placed and retrieved directly by specifying
the region number.

131
FILE ORGANIZATIONS
Contd....
• VSAM/ESDS
– Corresponds to CONSECUTIVE
• VSAM/KSDS
– Similar to EXTENDED.
– Empty spaces are created within dataset for later insertions.
– Deletion physically removes the record and creates space.
• VSAM/RRDS
– Records are located by relative record number used as a key.
– RRN starts with 1 and is incremented by 1 for each record.

132
DECLARING RECORD FILE
• FILE Attribute
– The first attribute that identifies the file name.
– Example : DCL filenm FILE RECORD

• EXTERNAL/INTERNAL Attribute
– INTERNAL means the file is known only within the
procedure block in which it is declared.
– EXTERNAL means it is known to other procedures too, and
is also declared as EXTERNAL there.

133
DECLARING RECORD FILE
Contd....
• INPUT/OUTPUT/UPDATE Attribute
– No default attribute
– UPDATE can be done only on DASD based files.
• BUFFERED/UNBUFFERED Attribute
– Buffer is an intermediate storage area through which data
records pass in an I/O operations.
– Records may not be blocked for an unbuffered attribute.
– Buffer size is of Block size.
– BUFFERS option in ENV attribute defines number of buffers
to be defined for the file.

134
DECLARING RECORD FILE
Contd....
• BACKWARDS Attribute
– Applicable to tape files, to indicate the processing is
backwards.
• KEYED Attribute
– Means record can be accessed by key.
– Applicable for INDEXED, VSAM/KSDS, and
REGIONAL files.

135
DECLARING RECORD FILE
Contd....
• SEQUENTIAL/DIRECT Attribute
– SEQUENTIAL means records are accessed in physical
sequence. For CONSECUTIVE, REGIONAL and VSAM
datasets, it is from first to last record, and for INDEXED it is
in the order of the key.
– DIRECT specifies records are accessed in random order. The
location of the record is determined by its key or region
number.

136
DECLARING RECORD FILE
Contd....

• ENVIRONMENT Attribute
– Specifies physical characteristics of the file indicated as
a list within parenthesis.
– Example : ENV(option list)
– Options are as in the next slide.

137
DECLARING RECORD FILE
________________________________________________________________ Contd....
Purpose Options Descriptions
______________________________________________
Record Format F/FB/V/VB/U
RECSIZE(rec size)
BLKSIZE(blk size)
No. of buffers BUFFERS(n)
File Organization CONSECUTIVE
INDEXED
VSAM
REGIONAL(1/2/3)
Tape Options LEAVE No rewind at end of file.
REREAD Rewind to the beginning.
Key SpecificationKEYLENGTH(n) Indexed, Regional 2 & 3.
KEYLOC(n) Start position in record for Indexed file.

138
DECLARING RECORD FILE
Contd....
___________________________________________________________
Purpose Options Descriptions
__________________________________________
Printer Options CTLASA Interpret control (first)
character before print.
CTL360 After print operation.
VSAM Options PASSWORD
BUFND(n) # of Data Buffers.
BUFNI(n) # of Index Buffers.
BUFSP(n) Total buffer size in bytes.
________________________________________________________________

139
EXAMPLE OF FILE DECLARATION

• An output file written on magnetic tape or disk. The


records are blocked variable length with a maximum size
of 100 (plus 4 bytes for record length). Block size of
1252.
• DCL FILE1 FILE RECORD OUTPUT SEQUENTIAL
BUFFERED EXTERNAL
ENV(CONSECUTIVE
VB BLKSIZE(1242) RECSIZE(104) BUFFERS(2));

140
EXAMPLE OF FILE DECLARATION
Contd....

• An INDEXED file accessed directly with the possibility


of updations. Records are blocked, key starts at 6th
byte and 7 byte long.
DCL FILE2 FILE RECORD UPDATE DIRECT UNBUFFERED
EXTERNAL KEYED ENV(INDEXED FB BLKSIZE(900)
RECSIZE(150) KEYLOC(5) KEYLENGTH(7));

141
EXAMPLE OF FILE DECLARATION

Contd....
• A Regional(1) data set to be read directly. Key is the
region number. File is known only to the current
procedure.
DCL FILE3 FILE RECORD INPUT DIRECT UNBUFFERED
INTERNAL KEYED ENV(REGIONAL(1) F
RECSIZE(267));

142
EXAMPLE OF FILE DECLARATION
Contd....

• A Regional(3) data set to be created in UNDEFINED


format. Key is relative track plus the control field that
identifies the record.
• DCL FILE4 FILE RECORD OUTPUT SEQUENTIAL

BUFFERED EXTERNAL KEYED ENV(REGIONAL(3) U


BLKSIZE(600) RECSIZE(600) KEYLENGTH(6));

143
EXAMPLE OF FILE DECLARATION
Contd....

• A VSAM file is to be created directly with the possibility


of rewriting selected records. Most of the options
required by VSAM are provided in the access method
services program.
• DCL FILE5 FILE RECORD UPDATE DIRECT
BUFFERED EXTERNAL KEYED
ENV(VSAM));

144
READ STATEMENT
• Sequential READ
READ FILE(filenm) INTO(area);
• Direct READ for REGIONAL(1) and RRDS file
READ FILE(filenm) INTO(area) KEY(region);
• Direct READ for REGIONAL(2) and REGIONAL(3) file
READ FILE(filenm) INTO(area) KEY(key || region);
• Direct READ for INDEXED and KSDS file
READ FILE(filenm) INTO(area) KEY(key);

145
WRITE STATEMENT
• For CONSECUTIVE and ESDS file
WRITE FILE(filenm) FROM(area);
• For REGIONAL(1) and RRDS file
WRITE FILE(filenm) FROM(area) KEYFROM(region);
• For REGIONAL(2) and REGIONAL(3) file
WRITE FILE(filenm) FROM(area) KEYFROM(key || region);
• For INDEXED and KSDS file
WRITE FILE(filenm) FROM(area) KEYFROM(key);

146
REWRITE STATEMENT
• For CONSECUTIVE and ESDS file, REWRITE updates last
read record
REWRITE FILE(filenm) FROM(area);
• For REGIONAL(1) and RRDS file
REWRITE FILE(filenm) FROM(area) KEY(region);
• For REGIONAL(2) and REGIONAL(3) file
REWRITE FILE(filenm) FROM(area) KEY(key || region);
• For INDEXED and KSDS file
REWRITE FILE(filenm) FROM(area) KEY(key);

147
DELETE STATEMENT
• For REGIONAL(1) and RRDS file
DELETE FILE(filenm) FROM(area) KEY(region);
• For REGIONAL(2) and REGIONAL(3) file
DELETE FILE(filenm) FROM(area) KEY(key || region);
• For INDEXED and KSDS file
DELETE FILE(filenm) FROM(area) KEY(key);

148
PROCESSING MODES
• There are two types of processing modes
1. MOVE mode
2. LOCATE mode
LOCATE OUTPUT_REC FILE(OUTPUT) SET(Q);

• FILE PROCESSING
• FILE ORGANIZATIONS
– CONSECUTIVE (Sequential)
– INDEXED (Indexed Sequential)
– REGIONAL (Direct or Random)

149
ACCESS TECHNIQUES
• Sequential
• Direct
Logical records can exist in 3 different formats.
These are :
– FIXED LENGTH (F)
– VARIABLE LENGTH(V)
– UNDEFINED LENGTH(U)
In the case of deletion, the first byte is flagged with a bit-string
of (8)’1’B.
The Record becomes a DUMMY record.
Typically, the first byte of a record declaration is set aside as a
‘flag’byte, the remaining bytes contain the key and other 150 data.
REGIONAL FILE

• There are 3 variations of REGIONAL file


organization.
– REGIONAL (1)
– REGIONAL (2)
– REGIONAL (3)

151
OPEN STATEMENTS - OPTIONS
• Following options can be specified along with OPEN
For DOS or DOS/VS PAGESIZE
INPUT/OUTPUT if UNBUFFERED
TITLE
For OS or OS/VS BUFFERED/UNBUFFERED
STREAM/RECORD
INPUT/OUTPUT/UPDATE
PRINT,LINESIZE,PAGESIZE
DIRECT/SEQUENTIAL
BACKWARDS
KEYED
TITLE

152
TYPES OF CONDITIONS
SOME CONDITIONS THAT MAY BE RAISED
DURING I/O OPERATION INCLUDE

1. ENDFILE(FILE NAME)
2. ENDPAGE(FILE NAME)
3. RECORD(FILE NAME)
4. TRANSMIT(FILE NAME)
5. CONVERSION
6. SIZE
e.g., ON ENDPAGE(PRINTR)
BEGIN; ............... END;
SIGNAL ENDPAGE(PRINTR);
153
ADDITIONAL TOPICS

• Stack and Array concepts


• PL/1 Debugging
• Pseudo Variables
• Receiving JCL paramters
• PL/1 Standards

154
STACK AND ARRAY
DCL NAME_STCK CTL CHAR(20);
DCL NAME_ARR(*) CTL CHAR(20);

READ FILE (I1FILE) INTO (I1AREA);

DO WHILE (I1EOFC_SW = NO);


ALLOC NAME_STCK;
NAME_STCK = ‘’;
NAME_STCK = I1AREA.NAME;
READ FILE (I1FILE) INTO (I1AREA);
END;
I = ALLOCN(NAME_STCK);
ALLOC NAME_ARR(I);
DO K = I TO 1 BY -1;
NAME_ARR(K) = NAME_STCK, BY NAME;
FREE NAME_STCK;
END;
155
DEBUGGING
• “CHECK” Option
– Main Programs, Internal Procedures, Statement level
– Output goes to Sysprint
– Variable values and internal procedures invoked are displayed.
• PUT DATA, PUT LIST statements
• PLITEST
• Other parameters aiding in Debugging
– Compiler Options - AGGREGATE, XREF
– Builtin functions - ONCHAR, ONLOC, ONSOURCE, ONCODE
– Conditions - SUBSCRIPTRANGE,SIZE

156
PSEUDO VARIABLES

• ONCHAR
• ONSOURCE
• STRING
• SUBSTR

157
RECEIVING JCL PARAMETERS

Main PL/1 routine receives parameter into it as varying


character string.
AVERAGE : PROC(PARMV) OPTIONS (MAIN);

DCL PARMV CHAR(100) VAR;


DCL PARMC CHAR(001);

PARMC = PARMV
IF VERIFY(PCHAR,’0123456789’) > 0 THEN
DO;
PUT SKIP LIST (“VALUE NOT NUMERIC”);
END; 158
PL/1 STANDARDS

• File naming conventions


• Internal variable naming conventions
• General
– Indentations
– Do’s and Dont’s

159
PROBLEM FOR THE CLASS

• Process the Employee_Skill file with following structure


Employee_Number N(6), Skill_Code A(5),
Years_Of_Experience N(2) And generate output report
• _____________________________________________
_
Sl. Skill No.of Total Yrs <2Yrs <4Yrs >4Yrs. % Org.
No. Code Emps of Exp. Total
______________________________________________

160
PROBLEM FOR THE CLASS
Contd....
For Example, if the file contents are:
Employee # Skill Code Experience
1 COBOL 8
1 C 5
1 PLI 3
2 C 8
2 COBOL 1
3 PLI 1
3 C 3
3 COBOL 3
161
PROBLEM FOR THE CLASS
Contd ......

Then the output report should be


_________________________________________________________
Sl. Skill No.of Total Yrs <2Yrs <4Yrs >=4Yrs. %Org.
No. Code Emps of Exp. Total
_________________________________________________

1 COBOL 3 12 1 1 1 37.5
2 C 3 16 0 1 2 50
3 PLI 2 4 1 1 0 12.5
_________________________________________________
162
THE PROGRAM
/*******************************************************/
/*Program to print SKILL Wise report extracting data from Employee_Skill File */
/*****************************************************************/
/*We will have three procedures. The Main SKILPRN,
Subroutine*/ /*SKILLOC to locate the SKILL in the array, and the
Function*/ /*EXP_PER for calculating the percentage of
experience.*/
SKILPRN : PROC OPTION(MAIN);
DCL END_OF_FILE BIT(1) INIT(‘0’B);
DCL 1 SKILL_REC,
2 EMPLOYEE_NUM PIC ‘999999’,
2 SKILL_CODE CHAR(5),
2 YEAR_OF_EXP PIC ‘99’;

163
THE PROGRAM
/* DECLARE THE ARRAY*/
DCL 1 SKILL_DATA(20),
2 SKILL CHAR(5),
2 OCC_NUM FIXED DEC(3),
2 TOTAL_YRS FIXED DEC(3),
2 PRCNTG_EXP FIXED DEC(5,2),
2 COUNT_2_YRS FIXED DEC(3),
2 COUNT_4_YRS FIXED DEC(3),
2 COUNT_MORE_YRS FIXED DEC(3);
DCL ORGNZN_EXP FIXED DEC(5) INIT(0);

/* DECLARE THE FILE*/


DCL EMPLOYEE_SKILL FILE RECORD INPUT
ENV(CONSECUTIVE F BLKSIZE(13) RECSIZE(13));

164
THE PROGRAM
Contd....
/*INITIALIZE THE ARRAY*/
DO I = 1 TO 20;
SKILL(I) = ‘ ‘;
OCC_NUM(I) = 0;
TOTAL_YRS(I) = 0;
PRCNTG_EXP(I) = 0;
COUNT_2_YRS(I) = 0;
COUNT_4_YRS(I) = 0;
COUNT_MORE_YRS(I) = 0;
END;

/* OPEN FILE & DEFINE END OF FILE CONDITION */


OPEN FILE(EMPLOYEE_SKILL) TITLE(‘EMPSKL’);
ON ENDFILE(EMPLOYEE_SKILL)
END_OF_FILE = ‘1’B;

165
THE PROGRAM Contd....
/* PROCESSING THE RECORDS*/

READ FILE(EMPLOYEE_SKILL) INTO(SKILL_REC);


DO WHILE ( END_OF_FILE);
CALL SKILLOC(SKILL_CODE);
OCC_NUM(I) = OCC_NUM(I) + 1;
ORGNZN_EXP = ORGNZN_EXP + YEARS_OF_EXP;
TOTAL_YRS(I) = TOTAL_YRS(I) + YEARS_OF_EXP;
IF YEARS_OF_EXP < 2
COUNT_2_YRS(I) = COUNT_2_YRS(I) + 1;
ELSE
IF YEARS_OF_EXP < 4
COUNT_4_YRS(I) = COUNT_4_YRS(I) + 1;
ELSE
COUNT_MORE_YRS(I) + 1;
READ FILE(EMPLOYEE_SKILL) INTO (SKILL_REC);
END;
166
THE PROGRAM
Contd....
/* PRINTING THE HEADING*/
PUT PAGE EDIT ((60)’-’)(COLUMN(10),A(60));
PUT SKIP EDIT(‘Sl. Skill No. Of Total Yrs <2 Yrs <4 Yrs >4 Yrs. Org.’)
(COLUMN(10),A);
PUT SKIP EDIT(‘No. Code Emps Of Exp. Total’)
(COLUMN(10),’A’);
/*PRINTING FOR EACH SKILL*/
DO I = 1 TO 20 WHILE(SKILL(I) =‘ ‘);
PRCNTG_EXP(I) = EXP_PER(TOTAL_YRS(I));
PUT SKIP EDIT(I, SKILL(I), OCC_NUM(I), TOTAL_YRS(I), COUNT_2_YRS(I),
COUNT_4_YRS(I), COUNT_MORE_YRS(I), PRCNTG_EXP(I))
(COLUMN(10), ‘Z9’,X(2),A(5),X(3),’ZZ9’,X(3),‘ZZZZ9’,X(3),
‘ZZ9’,X(3),’ZZ9’,X(3),’ZZ9’,X(3),’ZZ9.99’);
END;
PUT SKIP EDIT((60)’_’)(COLUMN(10),A(60));
CLOSE FILE(EMPLOYEE_SKILL);

167
THE PROGRAM
Contd....
/*SUB-PROCEDURE SKILLOC*/
SKILLOC: PROC(SK_CODE);
DCL SK_CODE CHAR(5);
/* ASSUMES THAT THERE CAN NOT BE MORE THAN 20 SKILLS*/
DO I=1 TO 20 WHILE(SKILL (I) = ‘ ’ | SKILL (I) = SK_CODE);
IF SKILL (I) = ‘ ’
SKILL (I) = SK_CODE;
EXIT;
END;
END SKILLOC;
/*SUB-PROCEDURE EXP-PER*/
EXP_PER : PROC(A) RETURNS(FIXED DECIMAL(5,2));
DCL A FIXED DEC(3);
RETURN((A * 100)/ORGNZN_EXP);
END EXP_PER;
*END SKILPRN;
168
THANK YOU

169

You might also like