HPlogo SORT-MERGE/XL Programmer's Guide: HP 3000 MPE/iX Computer Systems > Chapter 2 Creating Core Routines That Sort and Merge

Initializing a Sort or Merge

» 

Technical documentation

Complete book in PDF
» Feedback

 » Table of Contents

 » Index

You must specify the following to initialize the SORT-MERGE/XL utility and start the sorting or merging process:

  • input file(s)

  • output file

  • keys

  • data type and collating sequence

The HPSORTINIT intrinsic passes the information necessary to initialize the sorting process, and HPMERGEINIT passes the information to initialize the merging process.

The syntax for HPSORTINIT and HPMERGEINIT is:

   HPSORTINIT (status, inputfiles, outputfile, outputoption,

           reclength, numrecs, numkeys, keys, altseq, keycompare,

           errorproc, statistics, memsize, charseq);



   HPMERGEINIT (status, inputfiles, preprocessor, outputfile,

            postprocessor, keysonly, numkeys, keys, altseq,

            keycompare, errorproc, statistics, memsize, charseq);

The core routine examples that follow do not use all of the parameters listed above. When you enter these intrinsics, you must maintain the position of any unused parameters with commas.

The inputfiles and outputfiles parameters in this chapter are files; input and output by record is discussed in Chapter 3. The altseq and charseq parameters, which alter the collating sequence, are discussed in Chapter 4. The statistics parameters, which get SORT-MERGE/XL information, are discussed in Chapter 5

Specifying Input

The most common way to supply information to SORT-MERGE/XL is through an input file. Input considerations include:

  • Creating input files.

  • Accessing input files.

  • Dealing with tape input.

Creating Input Files

SORT-MERGE/XL accepts one or more input files. You create these files with an editor, in a program, or from a database. You may not use $NULL as an input file.

Remember that your data items must be in a fixed format. Each data item of the same type must start in the same column. If your data is not in a fixed format, your results will be unpredictable.

Creating Input Files in an Editor

You can use any text editor to create fixed format data files stored in character format.

For example, EDIT/3000 keeps your data items lined up by using tabs to separate them. This ensures a fixed format. To follow the example below, enter EDIT/3000, set the tab character to a displayable character, and indicate where you want the tabs to be set.

   /tabchar = "%", tabs = (21,41,61)

To verify that the tab character and the tabs are set the way you want them, enter:

   /verify tabchar, tabs

You will see EDIT/3000 display:

   tab character = "%"

   tabs = (21,41,61)

Enter your information in the following manner:

   \A

     1      Jones,%Eliza%000001%06/06/87

     2      Smith,%James%000005%06/06/87

     3      Jackson,%Johnathon%000005%06/06/87

     4      Washington,%Lois%000014%07/23/87

     5      Jackson,%Rosa%000022%08/15/87

When you list this file, it will appear as follows:

   Jones,              Eliza               000001              06/06/87

   Smith,              James               000002              06/06/87

   Jackson,            Johnathon           000003              06/06/87

   Washington,         Lois                000004              07/23/87

   Jackson,            Rosa                000005              08/15/87

Give your file a meaningful name, save it with the KEEP command. This example file contains permanent employee information, so it is kept as PERMEMP.

For more information about EDIT/3000, refer to the EDIT/3000 Reference Manual (03000-90012).

Creating Input Files in a Program

With intrinsics, you can access SORT-MERGE/XL programmatically from any language. When you create a file from a program, the data can be any data type allowed by the language you are using. For information about creating input files and saving the output file, refer to the programmer's guide for your language, such as:

  • HP Business BASIC/XL Reference Manual (32715-90001)

  • HP C Reference Manual (92434-90001) and HP C/XL Reference Manual Supplement (31506-90001)

  • COBOL II Reference Manual (31500-90001) and COBOL II/XL Reference Manual Supplement (31500-90005)

  • HP FORTRAN 77/XL Reference Manual (31501-90010)

  • HP Pascal Reference Manual (31502-90001)

When programming in COBOL, you can sort and merge records directly through the COBOL SORT and MERGE statements. These statements allow you to specify the key, collating sequence, and file or record output to be used by the SORT-MERGE/XL utility. For further information, refer to COBOL II Reference Manual (31500-90001) and COBOL II/XL Reference Manual Supplement (31500-90005)

You will find FORTRAN information in Appendix D of this manual.

Creating Input Files in a Database

Creating SORT-MERGE/XL input files from a database depends on your database and access method. For information about creating files and loading information to them from a database, refer to your database manual set.

Accessing the Input File

The SORT and MERGE intrinsics access input files through their file numbers. You pass the identification numbers in the inputfiles parameter of the HPSORTINIT or HPMERGEINIT intrinsic. The inputfiles parameter is an array. The last number in the array must be zero, as shown in the example below.

To get the file identification numbers, open the input files with the intrinsic HPFOPEN (or FOPEN), as in the example.

The following is from the example at the end of the chapter. The first part opens the files TEMPEMP and PERMEMP, and gets their file numbers, (tempFileNum and permFileNum). The second part puts these identification numbers in an array for the inputfiles parameter of the HPSORTINIT or HPMERGEINIT intrinsics.

   const

     designator : 2; {HPFOPEN formaldesignator= option}

     domain     : 3; {HPFOPEN file domain= option}

     access     : 11; {used later for output file}

   var

     tempFileNum  : INTEGER;    {HPFOPEN will return}

     permFileNum  : INTEGER;    { with file numbers }

     status       : INTEGER;        {error check}

     tempFile     : packed array [1..10] of CHAR;

     permFile     : packed array [1..10] of CHAR;

     permanent    : INTEGER;



   newFile := '%TEMPEMP%';

   permanent := 1;{file is permanent, in system file domain}

   HPFOPEN (tempFileNum, status, designator, tempFile,

            domain, permanent);



   PermFile := '%PERMEMP%';

   HPFOPEN (permFileNum, status, designator, permFile,

            domain, permanent);


Now that you have the file numbers, you can initialize the inputfiles parameter:

   var

     inputfiles   : array [1..3] of INTEGER;

        .

        .

        .

   inputfiles[1] :=  tempFileNum;  {from HPFOPEN}

   inputfiles[2] :=  permFileNum:  {from HPFOPEN}

   inputfiles[3] :=  0;   {last is always zero}

If you do not specify anything in inputfiles in HPSORTINIT, SORT/XL assumes that input will be by individual record and that you are using the HPSORTINPUT intrinsic. For information about input by record, refer to Chapter 3.

Using Tape Input (SORT/XL Only)

If any of your files are stored on tape, HPSORTINIT needs to know the total number of records that you have on disc and tape. This is specified in the numrecs parameter. If you have tape files but do not specify this parameter, SORT/XL defaults to 10,000 records per tape file. SORT/XL takes the size of your disc files from the file label; if all your files are on disc, do not specify this parameter.

Specifying Output

The most common method of maintaining output from SORT-MERGE/XL is by specifying an output file. Output considerations include:

  • Creating the Output File.

  • Output Record Format (SORT/XL).

  • Output Record Format (MERGE/XL).

Creating the Output File

You specify the output file by indicating its file identification number in the outputfile parameter in HPSORTINIT or HPMERGEINIT. The outputfile parameter is an array. The last number in the array must be zero, as shown in the example below.

To get the file number, open the output file with HPFOPEN (or FOPEN), as in the example.

The following is from the example at the end of the chapter. The first part creates a new permanent file named outFile with WRITE access and with logical records of 80 bytes. The second part puts the number of this file into an array for the outputfile parameter of the HPSORTINIT or HPMERGEINIT intrinsics.

   const

     designator = 2; {HPFOPEN formaldesignator= option}

     domain     = 3; {HPFOPEN file domain= option}

     access     = 11; {HPFOPEN access type option #3}

     record_size= 19;  {HPFOPEN record length option #3}

   var

     outFileNum   : INTEGER;

     status       : INTEGER;

     outFile      : packed array [1..10] of CHAR;

     new          : INTEGER;

     write        : INTEGER;

     size         : INTEGER;



   new := 4;  {creates a permanent file}

   write := 1; {file is write-only access}

   size := 80;

   outFile := '%ALLEMP%';

   HPFOPEN (outFileNum, status, designator, outFile,

            domain, new, access, write, record_size, size);

Now that you have the file numbers, you can initialize the inputfiles parameter:

   var

        outputfile   : array [1..2] of INTEGER;

            .

            .

            .

   outputfile[1] :=  outFileNum; {from HPFOPEN}

   outputfile[2] :=  0;

If you do not specify anything in outputfile SORT-MERGE/XL assumes that output will be by individual record and that you are using the HPSORTOUTPUT intrinsic.

Output Record Format (SORT/XL)

You specify the content of the SORT/XL output file with the outputoption parameter of HPSORTINIT. The part of the core routine example at the end of the chapter that specifys the outputoption in HPSORTINIT is:

   var

     OutputOption : INTEGER;

        .

        .

        .

   OutputOption := 0;  {Output record format same}

                       {  as input record format }

The options are outputoption equals:

0

Output record format is the same as the input record format (default).

1

Each output record contains a 32-bit integer in binary format that indicates the logical record number of the record.

The logical record number assigned to each record is the original record order (the first record is logical record zero, the second record is logical record one, and so on). If you use file input, the files are in the order that they are specified in inputfiles. For example, if the first file has three records, the logical record number of the fourth record in the second file is 6.

If you use file input, use the FCOPY utility to view the logical record numbers. For example, if your output file is ALLEMP, you use the FCOPY utility to print the value to the screen as follows:

   :FCOPY FROM = ALLEMP; TO= $STDLIST; HEX



   HP32212A.03.23 FILE COPIER (C) HEWLETT-PACKARD CO. 1984

The first two bytes of the record contain the logical record number. For example, logical record number three (final record number seven, the last of eight output records) is displayed as follows:

   ALLEMP RECORD 7 (%7, #7)

   0000: 0000 0003 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000

   000C: SAME: TO 0028-1

   EOF FOUND IN FROMFILE AFTER RECORD 7

   8 RECORDS PROCESSED *** 0 ERRORS



   END OF SUBSYSTEM

For more information about the FCOPY utility, refer to the FCOPY Reference Manual (03000-90064) .

If you use record output, use the ASCII intrinsic to convert the logical record number to a readable format. For details about the ASCII intrinsic, refer to the MPE XL Intrinsics Reference Manual (32650-90028).

2

Each output record contains only the key fields. The primary key is the leftmost; each succeeding key is to the right.

The following example shows the formats of the input and output records if the primary key is last name, the secondary key is employee number.

Figure 2-2 Input record format

   Jones,            Eliza               000001               06/06/87

Figure 2-3 Output record format

   Jones,             000001
3

Each output record contains the logical record number (in binary format) followed by the key fields.

The output format appears the same as for option 2, but the logical record number is stored in the first two bytes of the record. To view the logical record number, use the FCOPY utility or the ASCII intrinsic, as in outputoption = 1.

Output Record Format (MERGE/XL)

You specify the format of the MERGE/XL output file with the keysonly parameter of HPMERGEINIT.

The part of the core routine example at the end of the chapter that specifys the keysonly in HPSORTINIT is:

   var

     keysonly : INTEGER;

        .

        .

        .

   keysonly := 0;  {output record format same as}

                   {input record format}

The options are keysonly equals:

0

Output record format is the same as the input record format (default).

1

Each output record contains only the key fields. The primary key is the leftmost; each succeeding key is to the right.

The following example shows the record format for input and output records if the primary key is last name and the secondary key is employee number (keysonly =1):

Figure 2-4 Input record format

   Jones,             Eliza               000001              06/06/87

Figure 2-5 Output record format

   Jones,             000001

Specifying Keys

The key indicates the part of each record that is compared to determine output record order. In HPSORTINIT or HPMERGEINIT, you indicate the number of keys with the numkeys parameter and give key information in the keys parameter.

The part of the core routine example at the end of the chapter that specifies keys is:

   var

     num_keys :  INTEGER;

     keys : array [1..4] of INTEGER;

        .

        .

        .

   num_keys := 1;

   keys [1] := 1   {key begins}

   keys [2] := 20; {key length}

   keys [3] := 0;  {byte data }

   keys [4] := 0;  {ascending order}

The keys parameter is an integer array; each key contains four elements you specify. The elements are:

  • Beginning byte position of the key.

  • Number of bytes in the key.

  • Ordering sequence (0 for ascending, 1 for descending)

  • Type of data to be sorted by the key. See types below:

NOTE: Language equivalents of the following data types are given in Appendix E.
0

Byte (usually used).

If the key is ASCII or EBCDIC, it is byte type. With byte type, element 2 of keys contains the number of characters in the key.

1

twos complement format (integer).

This type is used for shortint (2-byte) or integer (4-byte) keys.

2

HP 3000 floating point.

This type is used for data that is in MPE V/E floating point format. This type is used for 4-byte and 8-byte real numbers.

3

IEEE standard floating point.

This type is used for data in the MPE XL floating point format. This type is used for 4-byte, 8-byte, or 16-byte real numbers.

4

Packed decimal with odd number of digits.

This is a COBOL data type.

5

Packed decimal with even number of digits.

This is a COBOL data type.

6

Display trailing sign.

This is a COBOL data type.

7

Display leading sign.

This is a COBOL data type.

8

Display trailing sign separate.

This is a COBOL data type.

9

Display leading sign separate.

This is a COBOL data type.

10

Character.

This data type is used in Native Language situations, when you have character data (as in option 0), but will be using a Native Language collating sequence.

11

Reserved.

12

Short floating point decimal.

This is an HP Business BASIC data type.

13

Floating point decimal.

This is an HP Business BASIC data type.

NOTE: The integrity of the keys array must be maintained throughout the sorting or merging operation. Do not change keys until after you have called HPSORTEND or HPMERGEEND.

Specifying Data and Sequence

If your key is a character or byte data type, you may use the altseq parameter to specify two things.

  • The data type of your input key.

  • The collating sequence (sort order) you want the sort to follow.

The altseq parameter is an array with two parts. The first element of the array is defined by the following table.

  Collating Sequence:
  ASCIIEBCDICALTERNATE
Data Type:ASCIICHR(255)CHR(2)CHR(0)
 EBCDICCHR(1)CHR(255)Undefined

The second element of altseq specifies the number of characters in the collating sequence. Remember that SORT-MERGE/XL begins counting on zero, and so you subtract one from the ordinary counting number. For example, ASCII has 256 characters, so the example below specifies the value 255.

The example at the end of this chapter uses ASCII input and ASCII sequence. The following is the part of the example that specifies the input data type and sorting sequence.

   var

     altseq : array [1..2] of CHAR;

        .

        .

        .

   altseq[1] := CHR(255); {data = ASCII,   }

                          {sequence = ASCII}

   altseq[2] := CHR(255); {256 characters in ASCII}

The altseq parameter is used only for byte data types The most common are ASCII and EBCDIC. Refer to Chapter 4 for information about alternate collating sequences, like one you design yourself or one of the predefined Native Language types.

Using HPSORTINIT

The following code segment demonstrates the the HPSORTINIT intrinsic. The parameters used are those already discussed.

   var

     status       : INTEGER;

     inputfiles   : array [1..3] of INTEGER;

     outputfile   : array [1..2] of INTEGER;

     outputoption : INTEGER;

     NumKeys      : INTEGER;

     keys         : array [1..4] of INTEGER;

     altseq       : array [1..2] of CHAR;



   inputfiles[1] :=  tempFileNum; {from HPFOPEN}

   inputfiles[2] :=  permFileNum: {from HPFOPEN}

   inputfiles[3] :=  0;



   outputfile[1] :=  outFileNum; {from HPFOPEN}

   outputfile[2] :=  0;



   outputOption := 0;  {output record format same}

                       { as input record format  }



   numKeys := 1;  {one key}

   keys[1] := 1;  {key begins}

   keys[2] := 20; {key length}

   keys[3] := 0;  {byte data}

   keys[4] := 0;  {ascending order}



   altseq[1] := CHR(255); {data = ASCII and}

                          {sequence = ASCII}

   altseq[2] := CHR(255); {256 characters in ASCII}



   HPSORTINIT (status, inputfiles, outputfile,

               outputOption,,, numKeys, keys, altseq,,,,,);

Using HPMERGEINIT

The following code segment demonstrates the HPMERGEINIT intrinsic. The parameters used are those already discussed.

Note that the HPMERGEINIT intrinsic call is different from the HPSORTINIT intrinsic call.

   var

     status       : INTEGER;

     inputfiles   : array [1..3] of INTEGER;

     outputfile   : array [1..2] of INTEGER;

     keysonly     : INTEGER;

     numKeys      : INTEGER;

     keys         : array [1..4] of INTEGER;

     altseq       : array [1..2] of CHAR;



   inputfiles[1] :=  tempFileNum; {from HPFOPEN}

   inputfiles[2] :=  permFileNum: {from HPFOPEN}

   inputfiles[3] :=  0;



   outputfile[1] :=  outFileNum;  {from HPFOPEN}

   outputfile[2] :=  0;



   keysonly  := 0;  {output record format same as }

                    {input record format          }



   numKeys := 1;  {one key}

   keys[1] := 41;  {key begins}

   keys[2] := 20; {key length}

   keys[3] := 0;  {byte data}

   keys[4] := 0;  {ascending order}



   altseq[1] := CHR(255); {data = ASCII           }

                          {sequence = ASCII       }

   altseq[2] := CHR(255); {256 characters in ASCII}



   HPMERGEINIT (status, inputfiles, outputfile,

                keysonly,,, numkeys, keys, altseq,,,,,);
Feedback to webmaster