Line sequential and Sequential
files are used very frequently in data processing applications.The records in these files are usually need to be put in
ascending or descending order for proper and easy handling. (Remember that
searching records in an ordered file ( a file sorted in respect to the field
which is the search key; you can stop reading records when you find a record
which has the key field having greater value than the value you
are searching for).Sorting a set of data which will fit into arrays (therefore
fitting into the memory) is quite easy. (Remember the Internal Sort
techniques you have learned in previous courses). Sorting large files which
will not fit into arrays in memory, on the other hand, is not easy. This
requires special algorithms to be implemented and generally called
"EXTERNAL SORT TECHNIQUES".Many COBOL compilers provide facilities to solve this
"Exteral Sort" problem. RM-COBOL, for instance, has a very powerful SORT
statement which will let the user SORT and MERGE sequential files very
easily.SORTING does not need much explanation here. The MERGE
process however, needs a little bit explanation.MERGING DATA is simply combining two or more SORTED
sequential files together into a single file so that the resulting file is also
sorted. We shall not cover the MERGE facility of the COBOL SORT statement in
this course.The RM-COBOL SORT Statement
The general syntax of the RM-COBOL SORT statement is :
SORT sort-work-file<span style=""> </span>ON {ASCENDING,DESCENDING} KEY data-name1, data-name2, ...</pre><pre><span style=""> </span>ON {ASCENDING,DESCENDING} KEY data-name1, data-name2, ...</pre><pre><span style=""> </span>USING file-to-be-sorted</pre><pre><span style=""> </span>GIVING sorted-file</pre>
<p class="MsoNormal">In order to use this SORT statement, you will have to
declare 3 files; </p>
<ol start="1" type="1"><li class="MsoNormal" style="">The WORK FILE for the
SORTING Process </li><li class="MsoNormal" style="">The file to be sorted
(input file) </li><li class="MsoNormal" style="">The file which will contain
the sorted records (output file) </li></ol>
<p class="MsoNormal">The WORK FILE is not an actual file; it is a special
declarative file structure with which you tell the compiler that you will
perform an external sort on a file and also indicate the fields on which you
shall set the sort criteria. The concept will become clearer with an example : </p>
<p class="MsoNormal">Suppose you have sequential file with personnel records
(PERSONEL.DAT) and you want to sort this file on the persons' names and
surnames. In order to use the SORT statement, you must declare a SORT WORK file
which will declare the structure of the records to be sorted and indicate the
lenghts and positions of the fields which contain the names and surnames: </p>
<p class="MsoNormal">Suppose that the structure of the PERSONEL.DAT file is : </p>
<pre><span style=""> </span>01<span style=""> </span>PERS-REC.</pre><pre><span style=""> </span>02 ID-NO<span style=""> </span>PIC X(8).</pre><pre><span style=""> </span>02 NAME<span style=""> </span>PIC X(16).</pre><pre><span style=""> </span>02 SURNAME<span style=""> </span>PIC X(16).</pre><pre><span style=""> </span>02 GENDER<span style=""> </span>PIC X.</pre><pre><span style=""> </span>02 DEPT-CODE<span style=""> </span>PIC X.</pre><pre><span style=""> </span>02 NCHILDREN<span style=""> </span>PIC 99.</pre><pre><span style=""> </span>02 HOME-ADR1<span style=""> </span>PIC X(25).</pre><pre><span style=""> </span>02 HOME-ADR2<span style=""> </span>PIC X(25).</pre><pre><span style=""> </span>02 HOME-ADR3<span style=""> </span>PIC X(25).</pre><pre><span style=""> </span>02 HOME-TEL<span style=""> </span>PIC X(12).</pre><pre><span style=""> </span>02 EMPLOYMENT-DATE.</pre><pre><span style=""> </span>03 R-DAY<span style=""> </span>PIC 99.</pre><pre><span style=""> </span>03 R-MONTH<span style=""> </span>PIC 99.</pre><pre><span style=""> </span>03 R-YEAR<span style=""> </span>PIC 9999.</pre><pre><span style=""> </span>02 LEAVE-DATE.</pre><pre><span style=""> </span>03 R-DAY<span style=""> </span>PIC 99.</pre><pre><span style=""> </span>03 R-MONTH<span style=""> </span>PIC 99.</pre><pre><span style=""> </span>03 R-YEAR<span style=""> </span>PIC 9999.</pre><pre><span style=""> </span>02 LEAVE-REASON<span style=""> </span>PIC X.</pre>
<p class="MsoNormal">The corresponding SORT WORK file declaration might look
something like : </p>
<pre><span style=""> </span>SD<span style=""> </span>SORT-WORK-FILE.</pre><pre><span style=""> </span>01<span style=""> </span>PERS-REC.</pre><pre><span style=""> </span>02 ID-NO<span style=""> </span><span style=""> </span>PIC X(8).</pre><pre><span style=""> </span>02 NAME<span style=""> </span>PIC X(16).</pre><pre><span style=""> </span>02 SURNAME<span style=""> </span>PIC X(16).</pre><pre><span style=""> </span>02 FILLER<span style=""> </span>PIC X(107).</pre>
<p class="MsoNormal">This declaration will enable the user to issue a SORT
statement using the NAME and SURNAME fields; into either <b><i>ascending </i></b>or
<b><i>descending </i></b>order. </p>
<p class="MsoNormal">Please note that a 108 byte filler is used in the
SORT-WORK-FILE record description so that the work file's record length matches
the record length of the PERSONEL.DAT file. </p>
<div align="center">
<table class="MsoNormalTable" style="" border="1" cellpadding="0">
<tbody><tr style="">
<td style="padding: 0.75pt;">
<p class="MsoNormal">Please also note the "SD" indicator used in place
of "FD" in <b>FILE SECTION</b>.<span style="font-size: 12pt;"><o:p></o:p></span></p>
</td>
</tr>
</tbody></table>
</div>
<p class="MsoNormal">You can perform sort on more than one field at time; and
also you can sort into ascending order (increasing) on one field, and into
descending order (decreasing) on another. For example, while sorting the
PERSONEL.DAT file, you can declare the NAME field to be the primary sort field
and the SURNAME to be the secondary sort field; so that records with identical
names will be sorted into surnames among themselves. (Just like the entries in
a telephone directory). </p>
<p class="MsoNormal">Referring to our PERSONEL.DAT example, typical file
declarations and a SORT statement would look like : </p>
<p class="MsoNormal">In the ENVIRONMENT DIVISION : </p>
<pre><span style=""> </span>INPUT-OUTPUT SECTION.</pre><pre><span style=""> </span>FILE-CONTROL.</pre><pre><span style=""> </span>SELECT SORT-FILE ASSIGN TO DISK "SORTWORK".</pre><pre><span style=""> </span>SELECT UNSORTED-PERSON ASSIGN TO DISK "PERSONEL.DAT"</pre><pre><span style=""> </span>ORGANIZATION IS LINE SEQUENTIAL.</pre><pre><span style=""> </span>SELECT SORTED-PERSON<span style=""> </span>ASSIGN TO DISK "SPERSONL.DAT"</pre><pre><span style=""> </span>ORGANIZATION IS LINE SEQUENTIAL.</pre>
<p class="MsoNormal">In the DATA DIVISION : </p>
<pre><span style=""> </span>FILE SECTION.</pre><pre><span style=""> </span>SD<span style=""> </span>SORT-FILE.</pre><pre><span style=""> </span>01<span style=""> </span>SORT-RECORD.</pre><pre><span style=""> </span>02<span style=""> </span>FILLER<span style=""> </span>PIC X(8).</pre><pre><span style=""> </span>02<span style=""> </span>S-NAME<span style=""> </span>PIC X(16).</pre><pre><span style=""> </span>02<span style=""> </span>S-SURNAME<span style=""> </span>PIC X(16).</pre><pre><span style=""> </span>02<span style=""> </span>FILLER<span style=""> </span>PIC X(108).</pre><pre><span style=""> </span></pre><pre><span style=""> </span><span style=""> </span>FD<span style=""> </span>UNSORTED-PERSON.</pre><pre><span style=""> </span>01<span style=""> </span>FILLER<span style=""> </span>PIC X(148).</pre><pre><o:p> </o:p></pre><pre><span style=""> </span>FD<span style=""> </span>SORTED-PERSON.</pre><pre><span style=""> </span>01<span style=""> </span>FILLER<span style=""> </span>PIC X(148).</pre>
<p class="MsoNormal">In the PROCEDURE DIVISION : </p>
<pre><span style=""> </span>SORT SORT-FILE</pre><pre><span style=""> </span>ON ASCENDING KEY S-NAME S-SURNAME</pre><pre><span style=""> </span>ON DESCENDING S-EMP-DATE</pre><pre><span style=""> </span>USING UNSORTED-PERSON</pre><pre><span style=""> </span>GIVING SORTED-PERSON.</pre>
<p class="MsoNormal"><b>Notes :</b> </p>
<ol start="1" type="1"><li class="MsoNormal" style="">The lengths of records in
the work, input and output files should match. </li><li class="MsoNormal" style="">You do not need to declare
the details of input and output file records if you do not need these
details in your program. </li><li class="MsoNormal" style="">You should not OPEN or
CLOSE the work, input and output files before, after or during the SORT
operation. </li><li class="MsoNormal" style="">You can specify more than
one sort field and the fields may have different ordering
(ascending/descending). </li></ol>
<p class="MsoNormal">A complete RM-COBOL program which sorts the PERSONEL.DAT
file, creating the sorted file SPERSONL.DAT might look like : </p>
<p class="MsoNormal"><b><i>Please study the SORT statement and its relevant file
declarations carefully! <o:p></o:p></i></b></p>
<pre><span style=""> </span>IDENTIFICATION DIVISION.</pre><pre><span style=""> </span>PROGRAM-ID.<span style=""> </span>"SORT DEMO".</pre><pre><span style=""> </span>ENVIRONMENT DIVISION.</pre><pre><span style=""> </span>CONFIGURATION SECTION.</pre><pre><span style=""> </span>SOURCE-COMPUTER.<span style=""> </span>RMCOBOL-85.</pre><pre><span style=""> </span>OBJECT-COMPUTER.<span style=""> </span>RMCOBOL-85.</pre><pre><span style=""> </span>INPUT-OUTPUT SECTION.</pre><pre><span style=""> </span>FILE-CONTROL.</pre><pre><span style=""> </span>SELECT SORT-FILE ASSIGN TO DISK "SORTWORK".</pre><pre><span style=""> </span>SELECT UNSORTED-PERSON ASSIGN TO DISK "PERSONEL.DAT"</pre><pre><span style=""> </span>ORGANIZATION IS LINE SEQUENTIAL.</pre><pre><span style=""> </span>SELECT SORTED-PERSON<span style=""> </span>ASSIGN TO DISK "SPERSONL.DAT"</pre><pre><span style=""> </span>ORGANIZATION IS LINE SEQUENTIAL.</pre><pre><span style=""> </span>*</pre><pre><span style=""> </span>DATA DIVISION.</pre><pre><span style=""> </span>FILE SECTION.</pre><pre><span style=""> </span>SD<span style=""> </span>SORT-FILE.</pre><pre><span style=""> </span>01<span style=""> </span>SORT-RECORD.</pre><pre><span style=""> </span>02<span style=""> </span>S-ID-NO<span style=""> </span>PIC X(8).</pre><pre><span style=""> </span>02<span style=""> </span>S-NAME<span style=""> </span>PIC X(16).</pre><pre><span style=""> </span>02<span style=""> </span>S-SURNAME<span style=""> </span>PIC X(16).</pre><pre><span style=""> </span>02<span style=""> </span>S-GENDER<span style=""> </span>PIC X.</pre><pre><span style=""> </span>02<span style=""> </span>S-DEPT-CODE<span style=""> </span>PIC 9.</pre><pre><span style=""> </span>02<span style=""> </span>FILLER<span style=""> </span>PIC X(89).</pre><pre><span style=""> </span>02<span style=""> </span>S-EMP-DATE.</pre><pre><span style=""> </span>03 S-EMP-DATE-DD<span style=""> </span>PIC 99.</pre><pre><span style=""> </span>03 S-EMP-DATE-MM<span style=""> </span>PIC 99.</pre><pre><span style=""> </span>03 S-EMP-DATE-YY<span style=""> </span>PIC 9999.</pre><pre><span style=""> </span>02<span style=""> </span>S-LEAVE-DATE.</pre><pre><span style=""> </span>03 S-LEAVE-DATE-DD<span style=""> </span>PIC 99.</pre><pre><span style=""> </span>03 S-LEAVE-DATE-MM<span style=""> </span>PIC 99.</pre><pre><span style=""> </span>03 S-LEAVE-DATE-YY<span style=""> </span>PIC 9999.</pre><pre><span style=""> </span>02<span style=""> </span>S-LEAVE-REASON<span style=""> </span>PIC X.</pre><pre><span style=""> </span></pre><pre><o:p> </o:p></pre><pre><span style=""> </span>FD<span style=""> </span>UNSORTED-PERSON.</pre><pre><span style=""> </span>01<span style=""> </span>FILLER<span style=""> </span>PIC X(148).</pre><pre><o:p> </o:p></pre><pre><span style=""> </span>FD<span style=""> </span>SORTED-PERSON.</pre><pre><span style=""> </span>01<span style=""> </span>FILLER<span style=""> </span>PIC X(148).</pre><pre><span style=""> </span>*</pre><pre><span style=""> </span>PROCEDURE DIVISION.</pre><pre><o:p> </o:p></pre><pre><span style=""> </span>MAIN-PGM.</pre><pre><o:p> </o:p></pre><pre><span style=""> </span>SORT SORT-FILE</pre><pre><span style=""> </span>ON ASCENDING KEY S-NAME S-SURNAME</pre><pre><span style=""> </span>ON DESCENDING S-EMP-DATE</pre><pre><span style=""> </span>USING UNSORTED-PERSON</pre><pre><span style=""> </span>GIVING SORTED-PERSON.</pre><pre><o:p> </o:p></pre><pre><span style=""> </span>STOP RUN.
Thursday, September 3, 2009