BBS: Inland Empire Archive Date: 04-17-93 (20:55) Number: 302 From: MARTY DUPLISSEY Refer#: NONE To: ALL Recvd: NO Subj: Useing index files Conf: (2) Quik_Bas
Blabbering as if I knew what I was talking about!
There has been some discussion about using sorted indexes.
Here is a function I
use to do this. You pass it a Key$ as a string and IDXNUM% as the
File handle you opened the index file with. It returns the Record Number to
look in the main data file to. I am using this with a large (4meg) data file
and it works extremely fast on my 386 40. It will return
the first match the first time you run it and the next one
the next time you run it etc. It will return 0 if no match
found.
DECLARE FUNCTION GetRecord& (IdxNum%, Key$)
TYPE indextype
Key AS STRING * 15
Record AS LONG
END TYPE '19
FUNCTION GetRecord& (IdxNum%, Key$) STATIC
DIM index AS indextype
A% = 0
'IF SECOND+ PASS THROUGH JUST Get the next one
IF Found% = 1 THEN
GET #IdxNum%, Middle&, index
IF LEFT$(index.Key, LEN(Key$)) = Key$ THEN
GetRecord& = index.Record
Middle& = Middle& + 1
EXIT FUNCTION
ELSE
GetRecord& = 0
Found% = 0
EXIT FUNCTION
END IF
END IF
'On more than 1st pass the folowing code is not run
'FIRST PASS THROUGH LETS INITILIZE EVERYTHING
Lower& = 1
IndexLen% = LEN(index)
Upper& = LOF(IdxNum%) / IndexLen%
TimesToLook% = 1
'Find out how many times to look before giving up
Counter& = Upper& / 2
DO UNTIL Counter& <= 1
Counter& = Counter& \ 2
TimesToLook% = TimesToLook% + 1
LOOP
' Lets find it
NoFind% = 0
DO WHILE Found% = 0
Middle& = (Lower& + Upper&) / 2
GET #IdxNum%, Middle&, index
SELECT CASE Key$
'Found it lets see if its the first occurance and if not step up
'Till it is
CASE LEFT$(index.Key, LEN(Key$))
DO UNTIL A% = 1
IF LEFT$(index.Key, LEN(Key$)) = Key$ THEN
Middle& = Middle& - 1
GET #IdxNum%, Middle&, index
ELSE
'Reset pointer to the last match
A% = 1
GET #IdxNum%, Middle& + 1, index
END IF
LOOP
Found% = 1
Middle& = Middle& + 1
'Exit found loop couse this is the last one
EXIT DO
CASE IS < LEFT$(index.Key, LEN(Key$))
Upper& = Middle& - 1
Found% = 0
NoFind% = NoFind% + 1
IF NoFind% = TimesToLook% THEN EXIT DO
CASE ELSE
Lower& = Middle& + 1
Found% = 0
NoFind% = NoFind% + 1
IF NoFind% = TimesToLook% THEN EXIT DO
END SELECT
LOOP
IF Found% = 0 THEN
GetRecord& = 0
ELSE
GetRecord& = index.Record&
'Reset middle for the next pass
Middle& = Middle& + 1
END IF
END FUNCTION
MARTY
--- GoldED 2.40
* Origin: World Link, Longview,TX.USA OS/2 (903-643-7607) (1:398/1)

Books at Amazon:
Back to BASIC: The History, Corruption, and Future of the Language
Hackers: Heroes of the Computer Revolution (including Tiny BASIC)
Go to: The Story of the Math Majors, Bridge Players, Engineers, Chess Wizards, Scientists and Iconoclasts who were the Hero Programmers of the Software Revolution
The Advent of the Algorithm: The Idea that Rules the World
Moths in the Machine: The Power and Perils of Programming
Mastering Visual Basic .NET