HPlogo SORT-MERGE/XL Programmer's Guide > Chapter 2 Creating Core Routines That Sort and Merge

Initializing a Sort or Merge

MPE documents

Complete PDF
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:

[add data]

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

[list data]

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.

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

  • HP C Reference Manual and HP C/XL Reference Manual Supplement

  • COBOL II Reference Manual and COBOL II/XL Reference Manual Supplement

  • HP FORTRAN 77/XL Reference Manual

  • HP Pascal Reference Manual

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 and COBOL II/XL Reference Manual Supplement.

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.

[code example]

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

[code example]

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.

[code]

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

[code]

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:

[code]

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:

[]

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:

[]

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.

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. This is the input record format:

[]

This is the output format:

[]

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:

[]

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). This is the input format:

[]

This is the output format:

[]

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:

[]

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.

Data Type Collating Sequence
ASCII EBCDIC ALTERNATE
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.

[]

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.

[]

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.

[]




Structure of the Core Routines


Ending Sorting or Merging