From: "Judson McClendon"
Subject: Re: DESPERATE: Indexed files in QBasic ??
Seamus BROWNE wrote:
> Hi,
> I have written a few progs with Qbasic.
> Some of them need to lookup data and find an equivalence:
> say , in a table with two fields Refno and Title,
> lookup the Refno "12345" and retrieve Title "War and Peace".
>
> I do this using DATA and READ to build a DIM table then
> run along down the DIM table till I find the Refno an dthen grab the Title.
>
> This may be a slow way of doing things but I can find any other way.
> So ...
> Q1. Is there no way to have an Indexed file in Qbasic ?
> Q2. Is there any other Basic that woul do the job OK without me having to
> re-write my progs?
A fairly straightforward way to do what you want is to maintain a sorted
index into your data file. The data file would be stored as a RANDOM file,
and the index (also a RANDOM file) would contain the key and the record
number of the data record the key refers to. As you add records, insert
their keys and record numbers into a sorted memory table of the new keys,
and at EOJ merge the old sorted index file with the sorted memory table,
creating an updated index. As you delete records, mark their index records
as deleted, or remove them from the memory table as appropriate. To find
a key do a binary search of the memory table first, then if the key is not
found, do a binary search of the index file. When the key is found, use
the record number to retrieve the data record. Except for the few seconds
it takes to merge the memory table with the old index, this is a very fast
and efficient solution. Because the memory table will be limited in size,
you must force an index update to clear the memory table if so many records
are added that it overflows.
If the data records are not too large you can keep the whole data file in
sequence using this method, but you would have only one search key. Using
index files, you can have multiple keys. If the index file is not too
large, you can read the entire index into memory when the file is opened,
maintain the index in memory, and write it out to disk when you close the
file.
--
Judson McClendon This is a faithful saying and worthy of all
Sun Valley Systems acceptance, that Christ Jesus came into the
judmczzz@mindspring.com world to save sinners (1 Timothy 1:15)
(please remove zzz from email id to respond)
*** QwkNews (tm) v2.1
* [TN71] Internet Newsgroup: alt.lang.powerbasic
--- GEcho 1.20/Pro
---------------
* Origin: Toast House Remote (1:100/561)
|