Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to format a While... Wend correctly?
#7
(02-27-2024, 01:41 AM)bplus Wrote: Yeah loading a dictionary file into an array takes awhile.

I took a dictionary file and made an Random access file of just the words. When I know which index the word is at then I know what the index the word and definition is at in the full file. Index same as Record number ie Get #1, recNumber...

For the Random access words I used a record length of 15 letters for the biggest word I could use.
That left word and definition records for the other Random access file I setup something like fixed string length of 237, no word and definition was greater than 237 letters long.

in setup for game play:
Code: (Select All)
Open "Collins_Word_List.RA" For Random As #1 Len = 15

So I used this code to find the word using Random Access for files where you get stuff by record number:
Code: (Select All)
Function Find& (x$) ' if I am using this only to find words in dictionary, I can mod to optimize
    ' the RA file is opened and ready for gets
    Dim As Long low, hi, test
    Dim w$
    If Len(x$) < 3 Then Exit Function ' words need to be 3 letters
    low = 1: hi = NTopWord
    While low <= hi
        test = Int((low + hi) / 2)
        Get #1, test, rec15
        w$ = _Trim$(rec15)
        If w$ = x$ Then
            Find& = test: Exit Function
        Else
            If w$ < x$ Then low = test + 1 Else hi = test - 1
        End If
    Wend
End Function

This is similar to that Hi Lo Game I showed a couple weeks ago for Binary Search.

Here is code I used to grab the definition when the user requested it:
Code: (Select All)
Function defineWord$ (w$) ' this will not edit out definitions that have () in them
    Dim nDef As Long
    w$ = UCase$(w$)
    nDef = Find&(w$)
    If nDef Then
        Open "Collins Words and Defs.RA" For Random As #2 Len = 237
        Get #2, nDef, rec237
        Close #2
    End If
    defineWord$ = _Trim$(rec237)
End Function

So I did not load the dictionary file into an array in the code and use RAM, I opened the Random Access file for the words at the start of the program and did the binary seach for words when needed. Again the index of the word was same as index to word and definition in the other Random Access file. It worked very well as there was no long delay at the start of the program to load the array and Binary Search for a word in a Random Access file did not take noticable time either.

So that method is something to consider that takes a little bit of work to set up the word and word plus definitions files but saves time when using the game.

rec15 and rec237 are fixed length strings I used for a record buffer for getting words or words with definitions.
NTopWord is the highest record number the files contained. NTopWord = 279496 for my Collins Dictionary after removing words longer than 15 letters.

@PhilOfPerth if you want I can zip the two RA files to you.

Thanks bplus.
It took me a while (I tend to lose track of things these days )  Rolleyes , but I worked through both the RA file builder and the search that you wrote, and after experimenting a bit I have a RA of words up to 9 letters (that's all I need). It takes a while to build the file, but once built it's very quick, quicker than my 26-file linear access version anyway.
.
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, W.A.) Big Grin
Please visit my Website at: http://oldendayskids.blogspot.com/
Reply


Messages In This Thread
RE: How to format a While... Wend correctly? - by PhilOfPerth - 02-29-2024, 02:22 AM



Users browsing this thread: 18 Guest(s)