You are here: Development Standards > Routines > General Routines > GENSORT.PUB

# GENSORT.PUB

GENSORT.PUB is a public program for allocating two types of dynamic Mkeyed sort files. The first type is a sort file with a single key. The second type is a temporary Mkeyed file that duplicates the file to be sorted but with a different set of alternate keys available.

`0100 ENTER CHAN,SRTPRM[ALL],STATUS,FILENAME$`

Variable | Description
CHAN | Channel number of the file to be sorted
SRTPRM[ALL] | Used to allocate key segments (described below)
STATUS | Set to -1 if the sort file allocation failed
FILENAME$ | Set to the file name of new sort file

Exit Points | Explanation
EXIT | GENSORT.PUB does not return any errors.

Here is an example of the data allocation and call statements:

`xxxx DIM L[1:16,1:5]`
`xxxx LET L[X,Y]=VALUE, etc....`
`xxxx CALL "GENSORT.PUB",FILE[F0],L[ALL],X,F$`
`xxxx LET F0=7; GOSUB GENOPEN`

To allocate a single-keyed sort file, assign the length of the key that you want the file sorted by to the first element of the L array−for example, L[1,1]=12. Call GENSORT and test X upon return. If the file was created, assign a file number to F0 and open it, using GENOPEN.

The file name will be "SCR"+FID(0), allowing for unique file names for all users. The file is created in the first data directory in the prefix, usually /osas/data. If the workstation default sort directory is used, the file will be placed in that directory first. The open and removal of this sort file must be done by the calling program.

The write statement for this type of sort file should look like this:

`xxxx WRITE (FILE[F0],KEY=KEY VARIABLE,ERR=GENERROR)`

No IOLIST is used for this file type. Only keys are stored in this file, which are made of concatenated pieces from the data. Some part of the key must point back to the original file in a unique fashion. If the initial read from the master file is made on one of the alternate keys, that key must still be the first part of the sort key to maintain that sort order. For example, to produce a report that lists employees by social security number and date of employment, the key length for the sort order would be 24 (11+7+6). The original file could be read by the alternate key for the social security number, but it still must be the first part of the sort key. The date of employment is next at a length of 7 for the Julian date, followed by the employee ID, which is the pointer back to the original file.

The second type of sort file is multi-keyed. Refer to the BBx manual for a complete description of Mkeyed files and defining keys. To allocate a multikeyed scratch file, assign the L array as follows (x is a number between 1 and 16):

L[x,1] is the key number you are defining and must be between 1 and 16. If the key number is the same for all 16 elements of the array, a single key with 16 concatenated segments is created. No key may exceed 64 bytes in length. The first or primary key must be unique.

L[x,2], the second element, must equal the number of the field in the original file. Its value should be between 1 and the number of the last field in the file. You can use a field number more than once.

L[x,3], the third element, must equal the starting position, or offset into the field, where the data will be found. Use a number between 1 and the length of the field. Keep in mind that the offset (L[x,3]) plus the length of the data piece (L[x,4], see below) must be satisfied; otherwise, an error will occur.

L[x,4], the fourth element, must equal the length of the data piece starting at the offset (L[x,3]) and cannot exceed the length of the field.

L[x,5], the fifth element, has three settings. If it is set to 1, the key is arranged in descending order. If it is set to 2, the key is designated as unique. If it is set to 3, the key is arranged in descending order. If the key is made from concatenated segments, this element should be set on the first segment only; otherwise, an error will occur and no allocation will be performed.

This procedure creates a data file about the same size as the original file.