Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to format a While... Wend correctly?
#41
words = CHR$(10) + _ReadFile("Collins.txt")
Reply
#42
(03-01-2024, 03:10 PM)SMcNeill Wrote: words = CHR$(10) + _ReadFile("Collins.txt")

of course! so simple Smile rechecking now...

This is working well for the few tests I gave it:
Code: (Select All)
Dim Shared words As String
'  be sure to to add Chr$(10) to assure catching the first word from dictionary
words = Chr$(10) + _ReadFile$("Collins.txt") ' Steve fix to catch first word at line start

Do
    Input "Enter word to lookup "; lookup$
    result$ = FindWord$(lookup$)
    If result$ = "" Then Print lookup$; " Not Found." Else Print result$
    Print
Loop Until lookup$ = ""

Function FindWord$ (wrd$) ' thanks mdijkens and Steve fix to assure start of line
    fpos& = InStr(words, Chr$(10) + UCase$(wrd$) + Chr$(9)) ' Steve fix to catch first word at line start
    If fpos& > 0 Then
        fpos& = fpos& + Len(wrd$) + 2
        eol& = InStr(fpos&, words, Chr$(13))
        FindWord$ = Mid$(words, fpos&, eol& - fpos&)
    End If
End Function

It still can't find Steve. Smile
b = b + ...
Reply
#43
WHILE "Pete" > "$teve"
    PRINT "Help, I'm stuck in an infinite loop!"
WEND

Pete  Big Grin
Reply
#44
I haven't analyzed all those listings, but this seems to me to be the simplest search:
Code: (Select All)
Open "RA.txt" For Random As #1 Len = 11
bot = 0: top = LOF(1) \ 11 + 2: found = 0
Input "Word"; wrd$
wrd$ = UCase$(wrd$)
While Abs(top - bot) > 1
    srch = Int((top + bot) / 2)
    Get #1, srch, a$
    a$ = UCase$(a$)
    If a$ = wrd$ Then Print wrd$; " found at"; srch: found = 1: Sleep 2
    If a$ < wrd$ Then ' too low
        bot = srch
    Else
        top = srch
    End If
Wend
Close
If found = 0 Then Print "Not found": Sleep 2
Cls
Run

... using the binary search that bplus showed (amended/maybe corrupted a bit).
Works for me, anyway, including AA, ZEBRA, ZZZS and CAT!

(RA.txt is an RA file with len = 11)
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
#45
@PhilOfPerth I have tested the method above and have to say I have been converted.

1. you don't have to do anything to Collins.txt but you could add an extra line first, no big deal if you don't. So no 2 RA files needed nor do you have to keep at least one RA file open while the program is running, not the greatest to have a file open when you quit though unlikely to do any damage. The entire file is loaded into a very long string and done in one giant gulp! You now have the contents accessible by RAM not file access, much faster.

2. Just look how it finds a word, it uses INSTR() ONCE because we enclose the word with a chr$(10) at front and a Chr$(9) on end to guarantee the words we find are the first on any line of Collins.txt file.
It does use INSTR() again just to find the end of that particlar line. So there is no playing of the guessing game that we do with an RA file. It's pretty cool efficiency wise.
b = b + ...
Reply




Users browsing this thread: 8 Guest(s)