Microsoft KB Archive/73041

= How to Use a Hashing Function to Insert Random File Records =

Article ID: 73041

Article Last Modified on 11/21/2006

-

APPLIES TO


 * Microsoft QuickBasic 2.0
 * Microsoft QuickBasic 2.01
 * Microsoft QuickBasic 3.0
 * Microsoft QuickBasic 4.0
 * Microsoft QuickBASIC 4.0b
 * Microsoft QuickBasic 4.5 for MS-DOS
 * Microsoft BASIC Compiler 6.0
 * Microsoft BASIC Compiler 6.0b
 * Microsoft BASIC Professional Development System 7.0
 * Microsoft BASIC Professional Development System 7.1

-



This article was previously published under Q73041



SUMMARY
This article discusses how to use an arbitrary hashing function to determine where to insert new records at unoccupied locations in a random access file.

This article applies to Microsoft QuickBasic versions 2.0, 2.01, 3.0, 4.0, 4.0b, and 4.5 for MS-DOS; to Microsoft Basic Compiler versions 6.0 and 6.0b for MS-DOS; and to Microsoft Basic Professional Development System (PDS) versions 7.0 and 7.1 for MS-DOS and MS OS/2.



MORE INFORMATION
An arbitrary hashing function is a function that returns a unique record index that is used as a starting record number to search for the next empty record in a random access file. In the program example below, the (arbitrary) hashing function returns the length of the name in each data record. (We could have added ASCII values of the characters within the string, or used any other arbitrary function, to produce a record number as a starting point for where to start searching for the next empty record.)

In the program example below, the hash number used to index the start of the search will be the length of the name in each record. Duplicate hash values are handled by incrementing the hash value by one, and trying to reinsert the record. If the record is unoccupied, the new data (name) is inserted. If the record is occupied, the hash value is once again incremented, and the test for an empty record once again takes place. This continues until an empty record is found and the new record information is inserted. If the hash value goes over 50 while looking for an open record number, the value is reset to 1 and the search continues. A loop counter stops the program if all 50 records in the file are full.

Program Example
' Works in QuickBasic versions 2.0, 2.01, 3.0, 4.0, 4.0b and 4.5; ' Microsoft Basic Compiler 6.0 and 6.0b; and Basic PDS 7.0 and 7.1.

OPEN &quot;test.dat&quot; FOR RANDOM AS #1 LEN = 19            ' open the file FIELD #1, 15 AS name$, 4 AS recnum$    'FIELD the random file buffer

FOR i = 1 TO 50 LSET name$ = &quot;unoccupied&quot;      ' Initialize all records in file LSET recnum$ = MKS$(i)         ' with the string &quot;unoccupied&quot;. PUT #1, i NEXT

FOR i = 1 TO 5 READ x$         ' Read names one at a time from the DATA statement hash% = LEN(x$)              ' Compute hash value GET #1, hash%                ' Get the record at hash% WHILE RTRIM$(name$) <> &quot;unoccupied&quot;  ' Check if currently occupied count = count + 1 IF count > 50 THEN PRINT &quot;File is full&quot;     ' Stop when file is full STOP END IF    IF hash% < 51 THEN     ' Cycle through all possible hash values hash% = hash% + 1   ' Increment hash value ELSE hash% = 1 END IF    GET #1, hash%    ' Get next record after incrementing hash value WEND LSET name$ = x$ LSET recnum$ = MKS$(hash%) PUT #1, hash% 'put data into file at the record pointed to by hash% NEXT

INPUT &quot;Enter new name: &quot;; n$  ' enter new value to put into file hash% = LEN(n$)

GET #1, hash%

WHILE RTRIM$(name$) <> &quot;unoccupied&quot;  ' Check if record at hash% is                                      ' occupied hash% = hash% + 1    ' Increment hash% GET #1, hash%        ' Get next record and check if occupied WEND

LSET name$ = n$ LSET recnum$ = MKS$(hash%)

PUT #1, hash%             ' Insert new data at unoccupied record

FOR j = 1 TO 50                 ' print the contents of the file GET #1, j IF RTRIM$(name$) = &quot;unoccupied&quot; THEN PRINT &quot;unoccupied&quot; ELSE PRINT name$ END IF IF j MOD 20 = 0 THEN SLEEP           ' stop to look at list ' In QB 2.x or 3.0, use INPUT Q$ instead of SLEEP NEXT END DATA Stan Jones, Rick Jones, Phil Jones, Bart Jones, Frank Jones

Additional query words: QuickBas BasicCom 4.00 4.00b 4.50 6.00 6.00b 2.00 3.00 7.00 7.10

Keywords: KB73041

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.