SEARCH and SORT [ HP Business BASIC/XL Migration Guide ] MPE/iX 5.0 Documentation
HP Business BASIC/XL Migration Guide
SEARCH and SORT
SEARCH (BASIC/260 FIND) and SORT are, for the most part, compatible with
the HP 260, and produce the expected results. There are some small
differences which may affect programs.
SEARCH
A special situation exists in BASIC/260 which does not exist in HP
Business BASIC/XL. This occurs when a function in the SEARCH expression
accesses the same workfile as SEARCH itself.
Consider the following piece of code:
WORKFILE IS #1
.
SEARCH USING Thread;FNSomething(#1) ! this would be FIND in BASIC/260
.
.
DEF FNSomething(#1)
READ #1;Pointer
.
FNEND
The HP 260 apparently keeps two different file control blocks so the FIND
statement does not disturb the user's file pointers. On the first call
to the function, the first record of the workfile is read.
In HP Business BASIC/XL this is not the case. SEARCH will read the first
pointer in the workfile and then, when the function is called, the
function will access the second record in the workfile.
To be compatible change the function to use a direct read. This can be
done by adding another parameter to the function call which passes the
number of the workfile record to read as shown:
Record=1
SEARCH USING Thread;FNSomething(#1,Record)
.
DEF FNSomething(#1,Record)
READ #1,Record;Pointer
Record=Record+1
.
FNEND
WORKFILES
Workfile Data File Type. You can only use binary files as workfiles for
SEARCH and SORT in HP Business BASIC/XL. BASIC/260 allows BASIC DATA
files as workfiles.
Workfile Pointers. The only supported data types in workfiles are 32 bit
integers. There is no fully compatible programming method possible,
since the HP 260 does not have 32 bit INTEGER data. It is recommended,
therefore, that workfiles be documented well to make the manual changes
easier.
Empty Workfiles. The WORKFILE IS statement does not define an empty
workfile. If a SEARCH finds no qualifying records, a marker is put in
the file saying that this file was used in a SEARCH but is empty. A
subsequent SORT or SORT ONLY recognizes this marker.
However, if a program performs a database search and uses PRINT to write
qualifying record pointers into the workfile, the marker is missing when
no records are found. A subsequent SORT looks at the workfile, does not
see the correct marker and so assumes that it must sort the whole
threadlist from the database. A subsequent SORT ONLY will fail because
it doesn't see the correct marker and believes the workfile is not valid.
Workfile Errors. Error number 239 (Workfile too small) does not exist in
HP Business BASIC/XL; it is replaced by the MPE file system error 59 (EOF
found).
Programs dealing with this kind of error have to be changed. To be more
compatible use a constant in your program header:
Wftoosmall = 239
...
IF ERRN = Wftoosmall THEN
...
When migrating the program, simply change the constant 239 to 59.
SIZE Function. As mentioned in the FILE I/O section, the SIZE function
no longer accepts a negative value as an argument. On the HP 260,
SIZE(-1) returns the logical record length of a workfile. Dividing this
value by 2 results in the THREAD length for the workfile.
HP Business BASIC/XL does not support the notion of a logical thread
length in a workfile. There is only a physical record length. Since the
THREAD statement is now a static statement, the length can clearly be
seen in the program.
For compatibility purposes, use a variable to remember the thread length
if necessary. Avoid use of the SIZE(-1) function.
Hints for SEARCH and SORT
The SEARCH and SORT statements are very expensive in terms of compiled
code space so it is best to limit their use within one subunit. Some
ways to decrease the number of uses are:
* For identical SEARCH/SORT statements, use a GOSUB routine.
* Move the complete SEARCH/SORT block out to a separate subunit.
* In the referenced IN DATASET USE statement, define only the variables
you really need. This can save a tremendous amount of code space.
* If you are searching an already existing workfile, use the FILTER
statement.
* If you are sorting an already existing workfile, use the SORT ONLY
statement.
FIND in SORT/260 can be used in the same flexible way as DBGET (see the
discussion in the previous section) with respect to IN DATA SET and
THREAD. The active specification at the time FIND/SORT is executed is
used.
In HP Business BASIC/XL, however, SEARCH and SORT require defined
references to a THREAD statement and an IN DATASET statement. These may
not be determined dynamically at run time due to the compiler's need to
generate the correct code at compile time.
MPE/iX 5.0 Documentation