COBOL – Subroutines ”; Previous Next Cobol subroutine is a program that can be compiled independently but cannot be executed independently. There are two types of subroutines: internal subroutines like Perform statements and external subroutines like CALL verb. Call Verb Call verb is used to transfer the control from one program to another program. The program that contains the CALL verb is the Calling Program and the program being called is known as the Called Program. Calling program execution will halt until the called program finishes the execution. Exit Program statement is used in the Called program to transfer the control back. Called Program Constraints Following are the called program requirements − Linkage section must be defined in the called program. It consists of data elements passed in the program. The data items should not have Value clause. PIC clause must be compatible with the variables passed through the calling program. Procedure division using has a list of variables passed from the calling program and the order must be same as mentioned in the Call verb. Exit program statement is used in the called program to transfer the control back. It must be the last statement in the called program. The parameters can be passed between programs in two ways − By Reference By Content Call By Reference If the values of variables in the called program are modified, then their new values will reflect in the calling program. If BY clause is not specified, then variables are always passed by reference. Syntax Following is the syntax of calling subroutine by reference − CALL sub-prog-name USING variable-1, variable-2. Example Following example is the MAIN calling program and UTIL is the called program − IDENTIFICATION DIVISION. PROGRAM-ID. MAIN. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-STUDENT-ID PIC 9(4) VALUE 1000. 01 WS-STUDENT-NAME PIC A(15) VALUE ”Tim”. PROCEDURE DIVISION. CALL ”UTIL” USING WS-STUDENT-ID, WS-STUDENT-NAME. DISPLAY ”Student Id : ” WS-STUDENT-ID DISPLAY ”Student Name : ” WS-STUDENT-NAME STOP RUN. Called Program IDENTIFICATION DIVISION. PROGRAM-ID. UTIL. DATA DIVISION. LINKAGE SECTION. 01 LS-STUDENT-ID PIC 9(4). 01 LS-STUDENT-NAME PIC A(15). PROCEDURE DIVISION USING LS-STUDENT-ID, LS-STUDENT-NAME. DISPLAY ”In Called Program”. MOVE 1111 TO LS-STUDENT-ID. EXIT PROGRAM. JCL to execute the above COBOL program − //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C //STEP1 EXEC PGM = MAIN When you compile and execute the above program, it produces the following result − In Called Program Student Id : 1111 Student Name : Tim Call By Content If the values of variables in the called program are modified, then their new values will not reflect in the calling program. Syntax Following is the syntax of calling subroutine by content − CALL sub-prog-name USING BY CONTENT variable-1, BY CONTENT variable-2. Example Following example is the MAIN calling program and UTIL is the called program − IDENTIFICATION DIVISION. PROGRAM-ID. MAIN. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-STUDENT-ID PIC 9(4) VALUE 1000. 01 WS-STUDENT-NAME PIC A(15) VALUE ”Tim”. PROCEDURE DIVISION. CALL ”UTIL” USING BY CONTENT WS-STUDENT-ID, BY CONTENT WS-STUDENT-NAME. DISPLAY ”Student Id : ” WS-STUDENT-ID DISPLAY ”Student Name : ” WS-STUDENT-NAME STOP RUN. Called Program IDENTIFICATION DIVISION. PROGRAM-ID. UTIL. DATA DIVISION. LINKAGE SECTION. 01 LS-STUDENT-ID PIC 9(4). 01 LS-STUDENT-NAME PIC A(15). PROCEDURE DIVISION USING LS-STUDENT-ID, LS-STUDENT-NAME. DISPLAY ”In Called Program”. MOVE 1111 TO LS-STUDENT-ID. EXIT PROGRAM. JCL to execute the above COBOL program − //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C //STEP1 EXEC PGM = MAIN When you compile and execute the above program, it produces the following result − In Called Program Student Id : 1000 Student Name : Tim Types of Call There are two types of calls − Static Call occurs when a program is compiled with the NODYNAM compiler option. A static called program is loaded into storage at compile time. Dynamic Call occurs when a program is compiled with the DYNAM and NODLL compiler option. A dynamic called program is loaded into storage at runtime. Print Page Previous Next Advertisements ”;
Category: cobol
COBOL – String Handling
COBOL – String Handling ”; Previous Next String handling statements in COBOL are used to do multiple functional operations on strings. Following are the string handling statements − Inspect String Unstring Inspect Inspect verb is used to count or replace the characters in a string. String operations can be performed on alphanumeric, numeric, or alphabetic values. Inspect operations are performed from left to right. The options used for the string operations are as follows − Tallying Tallying option is used to count the string characters. Syntax Following is the syntax of Tallying option − INSPECT input-string TALLYING output-count FOR ALL CHARACTERS The parameters used are − input-string − The string whose characters are to be counted. output-count − Data item to hold the count of characters. Example IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-CNT1 PIC 9(2) VALUE 0. 01 WS-CNT2 PIC 9(2) VALUE 0. 01 WS-STRING PIC X(15) VALUE ”ABCDACDADEAAAFF”. PROCEDURE DIVISION. INSPECT WS-STRING TALLYING WS-CNT1 FOR CHARACTER. DISPLAY “WS-CNT1 : “WS-CNT1. INSPECT WS-STRING TALLYING WS-CNT2 FOR ALL ”A”. DISPLAY “WS-CNT2 : “WS-CNT2 STOP RUN. JCL to execute the above COBOL program. //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C //STEP1 EXEC PGM = HELLO When you compile and execute the above program, it produces the following result − WS-CNT1 : 15 WS-CNT2 : 06 Replacing Replacing option is used to replace the string characters. Syntax Following is the syntax of Replacing option − INSPECT input-string REPLACING ALL char1 BY char2. The parameter used is − input-string − The string whose characters are to be replaced from char1 to char2. Example Live Demo IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-STRING PIC X(15) VALUE ”ABCDACDADEAAAFF”. PROCEDURE DIVISION. DISPLAY “OLD STRING : “WS-STRING. INSPECT WS-STRING REPLACING ALL ”A” BY ”X”. DISPLAY “NEW STRING : “WS-STRING. STOP RUN. JCL to execute the above COBOL program. //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C //STEP1 EXEC PGM = HELLO When you compile and execute the above program, it produces the following result − OLD STRING : ABCDACDADEAAAFF NEW STRING : XBCDXCDXDEXXXFF String String verb is used to concatenate the strings. Using STRING statement, two or more strings of characters can be combined to form a longer string. ‘Delimited By’ clause is compulsory. Syntax Following is the syntax of String verb − STRING ws-string1 DELIMITED BY SPACE ws-string2 DELIMITED BY SIZE INTO ws-destination-string WITH POINTER ws-count ON OVERFLOW DISPLAY message1 NOT ON OVERFLOW DISPLAY message2 END-STRING. Following are the details of the used parameters − ws-string1 and ws-string2 : Input strings to be concatenated ws-string : Output string ws-count : Used to count the length of new concatenated string Delimited specifies the end of string Pointer and Overflow are optional Example Live Demo IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-STRING PIC A(30). 01 WS-STR1 PIC A(15) VALUE ”Tutorialspoint”. 01 WS-STR2 PIC A(7) VALUE ”Welcome”. 01 WS-STR3 PIC A(7) VALUE ”To AND”. 01 WS-COUNT PIC 99 VALUE 1. PROCEDURE DIVISION. STRING WS-STR2 DELIMITED BY SIZE WS-STR3 DELIMITED BY SPACE WS-STR1 DELIMITED BY SIZE INTO WS-STRING WITH POINTER WS-COUNT ON OVERFLOW DISPLAY ”OVERFLOW!” END-STRING. DISPLAY ”WS-STRING : ”WS-STRING. DISPLAY ”WS-COUNT : ”WS-COUNT. STOP RUN. JCL to execute the above COBOL program − //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C //STEP1 EXEC PGM = HELLO When you compile and execute the above program, it produces the following result − WS-STRING : WelcomeToTutorialspoint WS-COUNT : 25 Unstring Unstring verb is used to split one string into multiple sub-strings. Delimited By clause is compulsory. Syntax Following is the syntax of Unstring verb − UNSTRING ws-string DELIMITED BY SPACE INTO ws-str1, ws-str2 WITH POINTER ws-count ON OVERFLOW DISPLAY message NOT ON OVERFLOW DISPLAY message END-UNSTRING. Example Live Demo IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-STRING PIC A(30) VALUE ”WELCOME TO TUTORIALSPOINT”. 01 WS-STR1 PIC A(7). 01 WS-STR2 PIC A(2). 01 WS-STR3 PIC A(15). 01 WS-COUNT PIC 99 VALUE 1. PROCEDURE DIVISION. UNSTRING WS-STRING DELIMITED BY SPACE INTO WS-STR1, WS-STR2, WS-STR3 END-UNSTRING. DISPLAY ”WS-STR1 : ”WS-STR1. DISPLAY ”WS-STR2 : ”WS-STR2. DISPLAY ”WS-STR3 : ”WS-STR3. STOP RUN. JCL to execute the above COBOL program − //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C //STEP1 EXEC PGM = HELLO When you compile and execute the above program, it produces the following result − WS-STR1 : WELCOME WS-STR2 : TO WS-STR3 : TUTORIALSPOINT Print Page Previous Next Advertisements ”;
COBOL – Quick Guide
COBOL – Quick Guide ”; Previous Next COBOL – Overview Introduction to COBOL COBOL is a high-level language. One must understand the way COBOL works. Computers only understand machine code, a binary stream of 0s and 1s. COBOL code must be converted into machine code using a compiler. Run the program source through a compiler. The compiler first checks for any syntax errors and then converts it into machine language. The compiler creates an output file which is known as load module. This output file contains executable code in the form of 0s and 1s. Evolution of COBOL During 1950s, when the businesses were growing in the western part of the world, there was a need to automate various processes for ease of operation and this gave birth to a high-level programming language meant for business data processing. In 1959, COBOL was developed by CODASYL (Conference on Data Systems Language). The next version, COBOL-61, was released in 1961 with some revisions. In 1968, COBOL was approved by ANSI as a standard language for commercial use (COBOL-68). It was again revised in 1974 and 1985 to develop subsequent versions named COBOL-74 and COBOL-85 respectively. In 2002, Object-Oriented COBOL was released, which could use encapsulated objects as a normal part of COBOL programming. Importance of COBOL COBOL was the first widely used high-level programming language. It is an English-like language which is user friendly. All the instructions can be coded in simple English words. COBOL is also used as a self-documenting language. COBOL can handle huge data processing. COBOL is compatible with its previous versions. COBOL has effective error messages and so, resolution of bugs is easier. Features of COBOL Standard Language COBOL is a standard language that can be compiled and executed on machines such as IBM AS/400, personal computers, etc. Business Oriented COBOL was designed for business-oriented applications related to financial domain, defense domain, etc. It can handle huge volumes of data because of its advanced file handling capabilities. Robust Language COBOL is a robust language as its numerous debugging and testing tools are available for almost all computer platforms. Structured Language Logical control structures are available in COBOL which makes it easier to read and modify. COBOL has different divisions, so it is easy to debug. COBOL – Environment Setup We have set up the COBOL Programming environment online, so that you can compile and execute all the available examples online. It gives you confidence in what you are reading and enables you to verify the programs with different options. Feel free to modify any example and execute it online. IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. PROCEDURE DIVISION. DISPLAY ”Hello World”. STOP RUN. For most of the examples given in this tutorial, you will find a Try it option in our website code sections at the top right corner that will take you to the online compiler. So just make use of it and enjoy your learning. Installing COBOL on Windows/Linux There are many Free Mainframe Emulators available for Windows which can be used to write and learn simple COBOL programs. One such emulator is Hercules, which can be easily installed on Windows by following a few simple steps as given below − Download and install the Hercules emulator, which is available from the Hercules” home site: www.hercules-390.eu Once you have installed the package on Windows machine, it will create a folder like C:/hercules/mvs/cobol. Run the Command Prompt (CMD) and reach the directory C:/hercules/mvs/cobol on CMD. The complete guide on various commands to write and execute a JCL and COBOL programs can be found at: www.jaymoseley.com/hercules/installmvs/instmvs2.htm Hercules is an open-source software implementation of the mainframe System/370 and ESA/390 architectures, in addition to the latest 64-bit z/Architecture. Hercules runs under Linux, Windows, Solaris, FreeBSD, and Mac OS X. A user can connect to a mainframe server in a number of ways such as thin client, dummy terminal, Virtual Client System (VCS), or Virtual Desktop System (VDS). Every valid user is given a login id to enter into the Z/OS interface (TSO/E or ISPF). Compiling COBOL Programs In order to execute a COBOL program in batch mode using JCL, the program needs to be compiled, and a load module is created with all the sub-programs. The JCL uses the load module and not the actual program at the time of execution. The load libraries are concatenated and given to the JCL at the time of execution using JCLLIB or STEPLIB. There are many mainframe compiler utilities available to compile a COBOL program. Some corporate companies use Change Management tools like Endevor, which compiles and stores every version of the program. This is useful in tracking the changes made to the program. //COMPILE JOB ,CLASS=6,MSGCLASS=X,NOTIFY=&SYSUID //* //STEP1 EXEC IGYCRCTL,PARM=RMODE,DYNAM,SSRANGE //SYSIN DD DSN=MYDATA.URMI.SOURCES(MYCOBB),DISP=SHR //SYSLIB DD DSN=MYDATA.URMI.COPYBOOK(MYCOPY),DISP=SHR //SYSLMOD DD DSN=MYDATA.URMI.LOAD(MYCOBB),DISP=SHR //SYSPRINT DD SYSOUT=* //* IGYCRCTL is an IBM COBOL compiler utility. The compiler options are passed using the PARM parameter. In the above example, RMODE instructs the compiler to use relative addressing mode in the program. The COBOL program is passed using the SYSIN parameter. Copybook is the library used by the program in SYSLIB. Executing COBOL Programs Given below is a JCL example where the program MYPROG is executed using the input file MYDATA.URMI.INPUT and produces two output files written to the spool. //COBBSTEP JOB CLASS=6,NOTIFY=&SYSUID // //STEP10 EXEC PGM=MYPROG,PARM=ACCT5000 //STEPLIB DD DSN=MYDATA.URMI.LOADLIB,DISP=SHR //INPUT1 DD DSN=MYDATA.URMI.INPUT,DISP=SHR //OUT1 DD SYSOUT=* //OUT2 DD SYSOUT=* //SYSIN DD * //CUST1 1000 //CUST2 1001 /* The load module of MYPROG is located in MYDATA.URMI.LOADLIB. This is important to note that the above JCL can be used for a non-DB2 COBOL module only. Executing COBOL-DB2 programs For running a COBOL-DB2 program, a specialized IBM utility is used in the JCL and the program; DB2 region and required parameters are passed as input to the utility. The steps followed in running a COBOL-DB2 program are as follows − When a COBOL-DB2 program is compiled, a DBRM (Database Request Module) is created along with the load module. The DBRM
COBOL – File Access Mode
COBOL – File Access Mode ”; Previous Next Till now, file organization schemes have been discussed. For each file organization scheme, different access modes can be used. Following are the types of file access modes − Sequential Access Random Access Dynamic Access The syntaxes in this module, mentioned along with their respective terms, only refer to their usage in the program. The complete programs using these syntaxes would be discussed in the next chapter. Sequential Access When the access mode is sequential, the method of record retrieval changes as per the selected file organization. For sequential files, records are accessed in the same order in which they were inserted. For indexed files, the parameter used to fetch the records are the record key values. For relative files, relative record keys are used to retrieve the records. Syntax Following is the syntax of sequential access mode − ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT file-name ASSIGN TO dd-name ORGANIZATION IS SEQUENTIAL ACCESS MODE IS SEQUENTIAL ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT file-name ASSIGN TO dd-name ORGANIZATION IS INDEXED ACCESS MODE IS SEQUENTIAL RECORD KEY IS rec-key1 ALTERNATE RECORD KEY IS rec-key2 ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT file-name ASSIGN TO dd-name ORGANIZATION IS RELATIVE ACCESS MODE IS SEQUENTIAL RELATIVE KEY IS rec-key1 Random Access When the access mode is RANDOM, the method of record retrieval changes as per the selected file organization. For indexed files, records are accessed according to the value placed in a key field which can be primary or alternate key. There can be one or more alternate indexes. For relative files , records are retrieved through relative record keys. Syntax Following is the syntax of random access mode − ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT file-name ASSIGN TO dd-name ORGANIZATION IS INDEXED ACCESS MODE IS RANDOM RECORD KEY IS rec-key1 ALTERNATE RECORD KEY IS rec-key2 ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT file-name ASSIGN TO dd-name ORGANIZATION IS RELATIVE ACCESS MODE IS RANDOM RELATIVE KEY IS rec-key1 Dynamic Access Dynamic access supports both sequential and random access in the same program. With dynamic access, one file definition is used to perform both sequential and random processing like accessing some records in sequential order and other records by their keys. With relative and indexed files, the dynamic access mode allows you to switch back and forth between sequential access mode and random access mode while reading a file by using the NEXT phrase on the READ statement. NEXT and READ functionalities will be discussed in the next chapter. Syntax Following is the syntax of dynamic access mode − ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT file-name ASSIGN TO dd-name ORGANIZATION IS SEQUENTIAL ACCESS MODE IS DYNAMIC RECORD KEY IS rec-key1 ALTERNATE RECORD KEY IS rec-key2 ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT file-name ASSIGN TO dd-name ORGANIZATION IS RELATIVE ACCESS MODE IS DYNAMIC RELATIVE KEY IS rec-key1 Print Page Previous Next Advertisements ”;
COBOL – File Handling
COBOL – File Handling ”; Previous Next The concept of files in COBOL is different from that in C/C++. While learning the basics of ”File” in COBOL, the concepts of both languages should not be corelated. Simple text files cannot be used in COBOL, instead PS (Physical Sequential) and VSAM files are used. PS files will be discussed in this module. To understand file handling in COBOL, one must know the basic terms. These terms only serve to understand the fundamentals of file handling. Further in depth terminology would be discussed in the chapter ”File Handling Verbs”. Following are the basic terms − Field Record Physical Record Logical Record File The following example helps in understanding these terms − Field Field is used to indicate the data stored about an element. It represents a single element as shown in the above example such as student id, name, marks, total marks, and percentage. The number of characters in any field is known as field size, for example, student name can have 10 characters. Fields can have the following attributes − Primary keys are those fields that are unique to each record and are used to identify a particular record. For example, in students marks file, each student will be having a unique student id which forms the primary key. Secondary keys are unique or non-unique fields that are used to search for related data. For example, in students marks file, full name of student can be used as secondary key when student id is not known. Descriptors fields are used to describe an entity. For example, in students marks file, marks and percentage fields that add meaning to the record are known descriptors. Record Record is a collection of fields that is used to describe an entity. One or more fields together form a record. For example, in students marks file, student id, name, marks, total marks, and percentage form one record. The cumulative size of all the fields in a record is known as the record size. The records present in a file may be of fixed length or variable length. Physical Record Physical record is the information that exists on the external device. It is also known as a block. Logical Record Logical record is the information used by the program. In COBOL programs, only one record can be handled at any point of time and it is called as logical record. File File is a collection of related records. For example, the students marks file consists of records of all the students. Print Page Previous Next Advertisements ”;
COBOL – File Handling Verbs
COBOL – File Handling Verbs ”; Previous Next File handling verbs are used to perform various operations on files. Following are the file handling verbs − Open Read Write Rewrite Delete Start Close Open Verb Open is the first file operation that must be performed. If Open is successful, then only further operations are possible on a file. Only after opening a file, the variables in the file structure are available for processing. FILE STATUS variable is updated after each file operation. Syntax OPEN “mode” file-name. Here, file-name is string literal, which you will use to name your file. A file can be opened in the following modes − Sr.No. Mode & Description 1 Input Input mode is used for existing files. In this mode, we can only read the file, no other operations are allowed on the file. 2 Output Output mode is used to insert records in files. If a sequential file is used and the file is holding some records, then the existing records will be deleted first and then new records will be inserted in the file. It will not happen so in case of an indexed file or a relative file. 3 Extend Extend mode is used to append records in a sequential file. In this mode, records are inserted at the end. If file access mode is Random or Dynamic, then extend mode cannot be used. 4 I-O Input-Output mode is used to read and rewrite the records of a file. Read Verb Read verb is used to read the file records. The function of read is to fetch records from a file. At each read verb, only one record can be read into the file structure. To perform a read operation, open the file in INPUT or I-O mode. At each read statement, the file pointer is incremented and hence the successive records are read. Syntax Following is the syntax to read the records when the file access mode is sequential − READ file-name NEXT RECORD INTO ws-file-structure AT END DISPLAY ”End of File” NOT AT END DISPLAY ”Record Details:” ws-file-structure END-READ. Following are the parameters used − NEXT RECORD is optional and is specified when an indexed sequential file is being read sequentially. INTO clause is optional. ws-file-structure is defined in the WorkingStorage Section to get the values from the READ statement. AT END condition becomes True when the end of file is reached. Example − The following example reads an existing file using line sequential organization. This program can be compiled and executed using Live Demo option where it will display all the records present in the file. IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT STUDENT ASSIGN TO ”input.txt” ORGANIZATION IS LINE SEQUENTIAL. DATA DIVISION. FILE SECTION. FD STUDENT. 01 STUDENT-FILE. 05 STUDENT-ID PIC 9(5). 05 NAME PIC A(25). WORKING-STORAGE SECTION. 01 WS-STUDENT. 05 WS-STUDENT-ID PIC 9(5). 05 WS-NAME PIC A(25). 01 WS-EOF PIC A(1). PROCEDURE DIVISION. OPEN INPUT STUDENT. PERFORM UNTIL WS-EOF=”Y” READ STUDENT INTO WS-STUDENT AT END MOVE ”Y” TO WS-EOF NOT AT END DISPLAY WS-STUDENT END-READ END-PERFORM. CLOSE STUDENT. STOP RUN. Suppose the input file data available in the input.txt file contains the following − 20003 Mohtashim M. 20004 Nishant Malik 20005 Amitabh Bachhan When you compile and execute the above program, it produces the following result − 20003 Mohtashim M. 20004 Nishant Malik 20005 Amitabh Bachhan Syntax Following is the syntax to write a record when the file access mode is random − READ file-name RECORD INTO ws-file-structure KEY IS rec-key INVALID KEY DISPLAY ”Invalid Key” NOT INVALID KEY DISPLAY ”Record Details: ” ws-file-structure END-READ. Example − The following example reads an existing file using indexed organization. This program can be compiled and executed using JCL on Mainframes where it will display all the records present in the file. On Mainframes server, we do not use text files; instead we use PS files. Let”s assume that the file present on Mainframes have same content as input.txt file in the above example. IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT STUDENT ASSIGN TO IN1 ORGANIZATION IS INDEXED ACCESS IS RANDOM RECORD KEY IS STUDENT-ID FILE STATUS IS FS. DATA DIVISION. FILE SECTION. FD STUDENT. 01 STUDENT-FILE. 05 STUDENT-ID PIC 9(5). 05 NAME PIC A(25). WORKING-STORAGE SECTION. 01 WS-STUDENT. 05 WS-STUDENT-ID PIC 9(5). 05 WS-NAME PIC A(25). PROCEDURE DIVISION. OPEN INPUT STUDENT. MOVE 20005 TO STUDENT-ID. READ STUDENT RECORD INTO WS-STUDENT-FILE KEY IS STUDENT-ID INVALID KEY DISPLAY ”Invalid Key” NOT INVALID KEY DISPLAY WS-STUDENT-FILE END-READ. CLOSE STUDENT. STOP RUN. JCL to execute the above COBOL program − //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C //STEP1 EXEC PGM = HELLO //IN1 DD DSN = STUDENT-FILE-NAME,DISP=SHR When you compile and execute the above program, it produces the following result − 20005 Amitabh Bachhan Write Verb Write verb is used to insert records in a file. Once the record is written, it is no longer available in the record buffer. Before inserting records into the file, move the values into the record buffer and then perform write verb. Write statement can be used with FROM option to directly write records from the working storage variables. From is an optional clause. If the access mode is sequential, then to write a record, the file must open in Output mode or Extend mode. If the access mode is random or dynamic, then to write a record, the file must open in Output mode or I-O mode. Syntax Following is the syntax to read a record when the file organization is sequential − WRITE record-buffer [FROM ws-file-structure] END-WRITE. Following is the syntax to read a record when the file organization is indexed or relative − WRITE record-buffer [FROM ws-file-structure] INVALID KEY DISPLAY ”Invalid Key” NOT INVALID KEY DISPLAY ”Record Inserted” END-WRITE. Example − The following example shows how to insert a new record in a new file when the organization is sequential. IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT STUDENT ASSIGN TO OUT1 ORGANIZATION
COBOL – Basic Verbs
COBOL Basic Verbs COBOL Tutorial COBOL – Home COBOL – Overview COBOL – Environment Setup COBOL – Program Structure COBOL – Basic Syntax COBOL – Data Types COBOL – Basic Verbs COBOL – Data Layout COBOL – Conditional Statements COBOL – Loop Statements COBOL – String Handling COBOL – Table Processing COBOL – File Handling COBOL – File Organization COBOL – File Access Mode COBOL – File Handling Verbs COBOL – Subroutines COBOL – Internal Sort COBOL – Database Interface COBOL Useful Resources COBOL – Questions and Answers COBOL – Quick Guide COBOL – Useful Resources COBOL – Basic Verbs Advertisements Previous Page Next Page COBOL verbs are used in the procedure division for data processing. A statement always start with a COBOL verb. There are several COBOL verbs with different types of actions. Input / Output Verbs Input/Output verbs are used to get data from the user and display the output of COBOL programs. The following two verbs are used for this process − Accept Verb Accept verb is used to get data such as date, time, and day from the operating system or directly from the user. If a program is accepting data from the user, then it needs to be passed through JCL. While getting data from the operating system, FROM option is included as shown in the following example − ACCEPT WS-STUDENT-NAME. ACCEPT WS-DATE FROM SYSTEM-DATE. Display Verb Display verb is used to display the output of a COBOL program. DISPLAY WS-STUDENT-NAME. DISPLAY “System date is : ” WS-DATE. COBOL PROGRAM IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-STUDENT-NAME PIC X(25). 01 WS-DATE PIC X(10). PROCEDURE DIVISION. ACCEPT WS-STUDENT-NAME. ACCEPT WS-DATE FROM DATE. DISPLAY “Name : ” WS-STUDENT-NAME. DISPLAY “Date : ” WS-DATE. STOP RUN. JCL to execute the above COBOL program − //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C //STEP1 EXEC PGM = HELLO //INPUT DD DSN=PROGRAM.DIRECTORY,DISP=SHR //SYSIN DD * TutorialsPoint /* When you compile and execute the above program, it produces the following result − Name : TutorialsPoint Date : 200623 Initialize Verb Initialize verb is used to initialize a group item or an elementary item. Data names with RENAME clause cannot be initialized. Numeric data items are replaced by ZEROES. Alphanumeric or alphabetic data items are replaced by SPACES. If we include REPLACING term, then data items can be initialized to the given replacing value as shown in the following example − Live Demo IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-NAME PIC A(30) VALUE ”ABCDEF”. 01 WS-ID PIC 9(5). 01 WS-ADDRESS. 05 WS-HOUSE-NUMBER PIC 9(3). 05 WS-COUNTRY PIC X(15). 05 WS-PINCODE PIC 9(6) VALUE 123456. PROCEDURE DIVISION. A000-FIRST-PARA. INITIALIZE WS-NAME, WS-ADDRESS. INITIALIZE WS-ID REPLACING NUMERIC DATA BY 12345. DISPLAY “My name is : “WS-NAME. DISPLAY “My ID is : “WS-ID. DISPLAY “Address : “WS-ADDRESS. DISPLAY “House Number : “WS-HOUSE-NUMBER. DISPLAY “Country : “WS-COUNTRY. DISPLAY “Pincode : “WS-PINCODE. STOP RUN. JCL to execute the above COBOL program − //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C //STEP1 EXEC PGM = HELLO When you compile and execute the above program, it produces the following result − My name is : My ID is : 12345 Address : 000 000000 House Number : 000 Country : Pincode : 000000 Move Verb Move verb is used to copy data from source data to destination data. It can be used on both elementary and group data items. For group data items, MOVE CORRESPONDING/CORR is used. In try it option, MOVE CORR is not working; but on a mainframe server, it will work. For moving data from a string, MOVE(x:l) is used where x is the starting position and l is the length. Data will be truncated if the destination data item PIC clause is less than the source data item PIC clause. If the destination data item PIC clause is more than the source data item PIC clause, then ZEROS or SPACES will be added in the extra bytes. The following example makes it clear. Live Demo IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-NUM1 PIC 9(9). 01 WS-NUM2 PIC 9(9). 01 WS-NUM3 PIC 9(5). 01 WS-NUM4 PIC 9(6). 01 WS-ADDRESS. 05 WS-HOUSE-NUMBER PIC 9(3). 05 WS-COUNTRY PIC X(5). 05 WS-PINCODE PIC 9(6). 01 WS-ADDRESS1. 05 WS-HOUSE-NUMBER1 PIC 9(3). 05 WS-COUNTRY1 PIC X(5). 05 WS-PINCODE1 PIC 9(6). PROCEDURE DIVISION. A000-FIRST-PARA. MOVE 123456789 TO WS-NUM1. MOVE WS-NUM1 TO WS-NUM2 WS-NUM3. MOVE WS-NUM1(3:6) TO WS-NUM4. MOVE 123 TO WS-HOUSE-NUMBER. MOVE ”INDIA” TO WS-COUNTRY. MOVE 112233 TO WS-PINCODE. MOVE WS-ADDRESS TO WS-ADDRESS1. DISPLAY “WS-NUM1 : ” WS-NUM1 DISPLAY “WS-NUM2 : ” WS-NUM2 DISPLAY “WS-NUM3 : ” WS-NUM3 DISPLAY “WS-NUM4 : ” WS-NUM4 DISPLAY “WS-ADDRESS : ” WS-ADDRESS DISPLAY “WS-ADDRESS1 : ” WS-ADDRESS1 STOP RUN. JCL to execute the above COBOL program. //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C //STEP1 EXEC PGM = HELLO When you compile and execute the above program, it produces the following result − WS-NUM1 : 123456789 WS-NUM2 : 123456789 WS-NUM3 : 56789 WS-NUM4 : 345678 WS-ADDRESS : 123INDIA112233 WS-ADDRESS1 : 123INDIA112233 Legal Moves The following table gives information about the legal moves − Alphabetic Alphanumeric Numeric Alphabetic Possible Possible Not Possible Alphanumeric Possible Possible Possible Numeric Not Possible Possible Possible Add Verb Add verb is used to add two or more numbers and store the result in the destination operand. Syntax Given below is the syntax to Add two or more numbers − ADD A B TO C D ADD A B C TO D GIVING E ADD CORR WS-GROUP1 TO WS-GROUP2 In syntax-1, A, B, C are added and the result is stored in C (C=A+B+C). A, B, D are added and the result is stored in D (D = A + B + D). In syntax-2, A, B, C, D are added and the result is stored in E (E=A+B+C+D). In syntax-3, sub-group items within WS-GROUP1 and WS-GROUP2 are added and the result is stored in WS-GROUP2. Example Live Demo IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-NUM1 PIC 9(9) VALUE 10 . 01 WS-NUM2
COBOL – Useful Resources
COBOL – Useful Resources ”; Previous Next The following resources contain additional information on COBOL. Please use them to get more in-depth knowledge on this topic. Useful Video Courses DB2 Online Training 11 Lectures 1.5 hours Tutorialspoint More Detail COBOL Online Training 13 Lectures 2.5 hours Tutorialspoint More Detail VSAM Online Training 9 Lectures 1 hours Tutorialspoint More Detail COBOL Programming Bootcamp: Master the Fundamentals 138 Lectures 11 hours Topictrick Education More Detail Learn Introductory Programming With COBOL 33 Lectures 3.5 hours Craig Kenneth Kaercher More Detail Print Page Previous Next Advertisements ”;
COBOL – Internal Sort
COBOL – Internal Sort ”; Previous Next Sorting of data in a file or merging of two or more files is a common necessity in almost all business-oriented applications. Sorting is used for arranging records either in ascending or descending order, so that sequential processing can be performed. There are two techniques which are used for sorting files in COBOL − External sort is used to sort files by using the SORT utility in JCL. We have discussed this in the JCL chapter. As of now, we will focus on internal sort. Internal sort is used to sort files within a COBOL program. SORT verb is used to sort a file. Sort Verb Three files are used in the sort process in COBOL − Input file is the file which we have to sort either in ascending or descending order. Work file is used to hold records while the sort process is in progress. Input file records are transferred to the work file for the sorting process. This file should be defined in the File-Section under SD entry. Output file is the file which we get after the sorting process. It is the final output of the Sort verb. Syntax Following is the syntax to sort a file − SORT work-file ON ASCENDING KEY rec-key1 [ON DESCENDING KEY rec-key2] USING input-file GIVING output-file. SORT performs the following operations − Opens work-file in I-O mode, input-file in the INPUT mode and output-file in the OUTPUT mode. Transfers the records present in the input-file to the work-file. Sorts the SORT-FILE in ascending/descending sequence by rec-key. Transfers the sorted records from the work-file to the output-file. Closes the input-file and the output-file and deletes the work-file. Example In the following example, INPUT is the input file which needs to be sorted in ascending order − IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT INPUT ASSIGN TO IN. SELECT OUTPUT ASSIGN TO OUT. SELECT WORK ASSIGN TO WRK. DATA DIVISION. FILE SECTION. FD INPUT. 01 INPUT-STUDENT. 05 STUDENT-ID-I PIC 9(5). 05 STUDENT-NAME-I PIC A(25). FD OUTPUT. 01 OUTPUT-STUDENT. 05 STUDENT-ID-O PIC 9(5). 05 STUDENT-NAME-O PIC A(25). SD WORK. 01 WORK-STUDENT. 05 STUDENT-ID-W PIC 9(5). 05 STUDENT-NAME-W PIC A(25). PROCEDURE DIVISION. SORT WORK ON ASCENDING KEY STUDENT-ID-O USING INPUT GIVING OUTPUT. DISPLAY ”Sort Successful”. STOP RUN. JCL to execute the above COBOL program − //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C //STEP1 EXEC PGM = HELLO //IN DD DSN = INPUT-FILE-NAME,DISP = SHR //OUT DD DSN = OUTPUT-FILE-NAME,DISP = SHR //WRK DD DSN = &&TEMP When you compile and execute the above program, it produces the following result − Sort Successful Merge Verb Two or more identically sequenced files are combined using Merge statement. Files used in the merge process − Input Files − Input-1, Input-2 Work File Output File Syntax Following is the syntax to merge two or more files − MERGE work-file ON ASCENDING KEY rec-key1 [ON DESCENDING KEY rec-key2] USING input-1, input-2 GIVING output-file. Merge performs the following operations − Opens the work-file in I-O mode, input-files in the INPUT mode and output-file in the OUTPUT mode. Transfers the records present in the input-files to the work-file. Sorts the SORT-FILE in ascending/descending sequence by rec-key. Transfers the sorted records from the work-file to the output-file. Closes the input-file and the output-file and deletes the work-file. Example In the following example, INPUT1 and INPUT2 are the input files which are to be merged in ascending order − IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT INPUT1 ASSIGN TO IN1. SELECT INPUT2 ASSIGN TO IN2. SELECT OUTPUT ASSIGN TO OUT. SELECT WORK ASSIGN TO WRK. DATA DIVISION. FILE SECTION. FD INPUT1. 01 INPUT1-STUDENT. 05 STUDENT-ID-I1 PIC 9(5). 05 STUDENT-NAME-I1 PIC A(25). FD INPUT2. 01 INPUT2-STUDENT. 05 STUDENT-ID-I2 PIC 9(5). 05 STUDENT-NAME-I2 PIC A(25). FD OUTPUT. 01 OUTPUT-STUDENT. 05 STUDENT-ID-O PIC 9(5). 05 STUDENT-NAME-O PIC A(25). SD WORK. 01 WORK-STUDENT. 05 STUDENT-ID-W PIC 9(5). 05 STUDENT-NAME-W PIC A(25). PROCEDURE DIVISION. MERGE WORK ON ASCENDING KEY STUDENT-ID-O USING INPUT1, INPUT2 GIVING OUTPUT. DISPLAY ”Merge Successful”. STOP RUN. JCL to execute the above COBOL program − //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C //STEP1 EXEC PGM = HELLO //IN1 DD DSN=INPUT1-FILE-NAME,DISP=SHR //IN2 DD DSN=INPUT2-FILE-NAME,DISP=SHR //OUT DD DSN = OUTPUT-FILE-NAME,DISP=SHR //WRK DD DSN = &&TEMP When you compile and execute the above program, it produces the following result − Merge Successful Print Page Previous Next Advertisements ”;
COBOL – Table Processing
COBOL – Table Processing ”; Previous Next Arrays in COBOL are known as tables. An array is a linear data structure and is a collection of individual data items of same type. Data items of a table are internally sorted. Table Declaration Table is declared in Data Division. Occurs clause is used to define a table. Occurs clause indicates the repetition of data name definition. It can be used only with level numbers starting from 02 to 49. Do not use occurs clause with Redefines. Description of one-dimensional and two-dimensional table is as follows − One-Dimensional Table In a one-dimensional table, occurs clause is used only once in declaration. WSTABLE is the group item that contains table. WS-B names the table elements that occur 10 times. Syntax Following is the syntax for defining a one-dimensional table − 01 WS-TABLE. 05 WS-A PIC A(10) OCCURS 10 TIMES. Example Live Demo IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-TABLE. 05 WS-A PIC A(10) VALUE ”TUTORIALS” OCCURS 5 TIMES. PROCEDURE DIVISION. DISPLAY “ONE-D TABLE : “WS-TABLE. STOP RUN. JCL to execute the above COBOL program − //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C //STEP1 EXEC PGM = HELLO When you compile and execute the above program, it produces the following result − ONE-D TABLE : TUTORIALS TUTORIALS TUTORIALS TUTORIALS TUTORIALS Two-Dimensional Table A two-dimensional table is created with both data elements being variable length. For reference, go through the syntax and then try to analyze the table. The first array (WS-A) can occur from 1 to 10 times and the inner array (WS-C) can occur from 1 to 5 times. For each entry of WS-A, there will be corresponding 5 entries of WS-C. Syntax Following is the syntax for defining a two-dimensional table − 01 WS-TABLE. 05 WS-A OCCURS 10 TIMES. 10 WS-B PIC A(10). 10 WS-C OCCURS 5 TIMES. 15 WS-D PIC X(6). Example Live Demo IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-TABLE. 05 WS-A OCCURS 2 TIMES. 10 WS-B PIC A(10) VALUE ” TUTORIALS”. 10 WS-C OCCURS 2 TIMES. 15 WS-D PIC X(6) VALUE ” POINT”. PROCEDURE DIVISION. DISPLAY “TWO-D TABLE : “WS-TABLE. STOP RUN. JCL to execute the above COBOL program − //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C //STEP1 EXEC PGM = HELLO When you compile and execute the above program, it produces the following result − TWO-D TABLE : TUTORIALS POINT POINT TUTORIALS POINT POINT Subscript Table individual elements can be accessed by using subscript. Subscript values can range from 1 to the number of times the table occurs. A subscript can be any positive number. It does not require any declaration in data division. It is automatically created with occurs clause. Example Live Demo IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-TABLE. 05 WS-A OCCURS 3 TIMES. 10 WS-B PIC A(2). 10 WS-C OCCURS 2 TIMES. 15 WS-D PIC X(3). PROCEDURE DIVISION. MOVE ”12ABCDEF34GHIJKL56MNOPQR” TO WS-TABLE. DISPLAY ”WS-TABLE : ” WS-TABLE. DISPLAY ”WS-A(1) : ” WS-A(1). DISPLAY ”WS-C(1,1) : ” WS-C(1,1). DISPLAY ”WS-C(1,2) : ” WS-C(1,2). DISPLAY ”WS-A(2) : ” WS-A(2). DISPLAY ”WS-C(2,1) : ” WS-C(2,1). DISPLAY ”WS-C(2,2) : ” WS-C(2,2). DISPLAY ”WS-A(3) : ” WS-A(3). DISPLAY ”WS-C(3,1) : ” WS-C(3,1). DISPLAY ”WS-C(3,2) : ” WS-C(3,2). STOP RUN. JCL to execute the above COBOL program − //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C //STEP1 EXEC PGM = HELLO When you compile and execute the above program, it produces the following result − WS-TABLE : 12ABCDEF34GHIJKL56MNOPQR WS-A(1) : 12ABCDEF WS-C(1,1) : ABC WS-C(1,2) : DEF WS-A(2) : 34GHIJKL WS-C(2,1) : GHI WS-C(2,2) : JKL WS-A(3) : 56MNOPQR WS-C(3,1) : MNO WS-C(3,2) : PQR Index Table elements can also be accessed using index. An index is a displacement of element from the start of the table. An index is declared with Occurs clause using INDEXED BY clause. The value of index can be changed using SET statement and PERFORM Varying option. Syntax Following is the syntax for defining Index in a table − 01 WS-TABLE. 05 WS-A PIC A(10) OCCURS 10 TIMES INDEXED BY I. Example Live Demo IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-TABLE. 05 WS-A OCCURS 3 TIMES INDEXED BY I. 10 WS-B PIC A(2). 10 WS-C OCCURS 2 TIMES INDEXED BY J. 15 WS-D PIC X(3). PROCEDURE DIVISION. MOVE ”12ABCDEF34GHIJKL56MNOPQR” TO WS-TABLE. PERFORM A-PARA VARYING I FROM 1 BY 1 UNTIL I >3 STOP RUN. A-PARA. PERFORM C-PARA VARYING J FROM 1 BY 1 UNTIL J>2. C-PARA. DISPLAY WS-C(I,J). JCL to execute the above COBOL program − //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C //STEP1 EXEC PGM = HELLO When you compile and execute the above program, it produces the following result − ABC DEF GHI JKL MNO PQR Set Statement Set statement is used to change the index value. Set verb is used to initialize, increment, or decrement the index value. It is used with Search and Search All to locate elements in table. Syntax Following is the syntax for using a Set statement − SET I J TO positive-number SET I TO J SET I TO 5 SET I J UP BY 1 SET J DOWN BY 5 Example Live Demo IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-TABLE. 05 WS-A OCCURS 3 TIMES INDEXED BY I. 10 WS-B PIC A(2). 10 WS-C OCCURS 2 TIMES INDEXED BY J. 15 WS-D PIC X(3). PROCEDURE DIVISION. MOVE ”12ABCDEF34GHIJKL56MNOPQR” TO WS-TABLE. SET I J TO 1. DISPLAY WS-C(I,J). SET I J UP BY 1. DISPLAY WS-C(I,J). STOP RUN. JCL to execute the above COBOL program. //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C //STEP1 EXEC PGM = HELLO When you compile and execute the above program, it produces the following result − ABC JKL Search Search is a linear search method, which is used to find elements inside the table. It can be performed on sorted as well as unsorted table. It is used only for tables declared by Index phrase. It starts with the initial value of index. If the searched element is not found,