QB64 Phoenix Edition
How can I create a search file for text? - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1)
+--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3)
+---- Forum: Help Me! (https://qb64phoenix.com/forum/forumdisplay.php?fid=10)
+---- Thread: How can I create a search file for text? (/showthread.php?tid=2406)



How can I create a search file for text? - PhilOfPerth - 01-25-2024

How can I create a quick search (e.g. binary search) of a text file using QBPE? My file is several thousand (sorted) items of very varied length. 
Just an algorithm will do, I'm happy to try to  code the function myself (although I may need a few prompts along the way). Big Grin


RE: How can I create a search file for text? - bplus - 01-25-2024

Binary search of an array in alpha order call it arr$()

Mark the first item low = 1 mark the last item high = number of items

recalcMiddle:

if low > high then the item is not found in arr$() and you are done with unsuccessful search.

then middle = int(low + high)/2

now if the arr$(middle) > search item then move high to middle - 1 goto recalcMiddle

now if arr$(middle) < search item then low = middle + 1 goto recalcMiddle

now if arr$(middle) = search item then you've found it and are done!

You play the game, guess my number from 1 to 100, in just the same way!


RE: How can I create a search file for text? - PhilOfPerth - 01-25-2024

Great! Thanks bplus, I knew you'd come through!
That's clear enough even for me.
Now, back to work (or play) fo me.


RE: How can I create a search file for text? - bplus - 01-25-2024

Good luck!


RE: How can I create a search file for text? - SMcNeill - 01-26-2024

Another very simple way is to just read the whole file into one string, and then use INSTR with your search term.


RE: How can I create a search file for text? - bplus - 01-26-2024

(01-26-2024, 12:10 AM)SMcNeill Wrote: Another very simple way is to just read the whole file into one string, and then use INSTR with your search term.

Oh yeah that's nice too!

here I made a little AI Game to watch for Guess Number:
Code: (Select All)
'2024-01-25 update
DefInt A-Z
While 1
    Hi = 101: Lo = 0: number = Int(Rnd * 100) + 1
    ' number = 1  'test limits
    ' number = 100
    Do
        guess = Int((Hi + Lo) / 2)
        Print "AI guesses the number ("; _Trim$(Str$(number)); ") as"; guess
        If guess < number Then Print "low": Lo = guess + 1
        If guess > number Then Print "high": Hi = guess - 1
    Loop Until guess = number
    Input "Yeah, press enter for another..."; w$
    Cls
Wend