I was wondering if anyone knows of a function in Xojo or a bit of code that will allow me to do and matching from user input to compare with a string. I have a multidimensional array and column 3 has a description which is about 3000 characters long and about 400 rows. I need to allow my users to enter multiple words as input e.g. “hdd add 3tb sata” and for the function to only give me the rows where all of the words exist in the description but each word can be anywhere in the description but they all have to exist. I have tried to write something using insert but it is not working reliably and as this is something common I thought someone may have a better suggestion or way of doing it. I had though about using RegEx but not sure if it is possible to do this type of thing in regex or not.
I would start by making a Join of the array so I have a big string where I can verify a word exists there with Instr() or a Regex.
Then use IndexOf to find the exact element where the search appears http://documentation.xojo.com/index.php/IndexOf
Ok I will give that a shot, thanks
or convert it to a database table and use SQL functions…
SELECT * FROM <table> WHERE <string> LIKE '% hdd %' OR <string> LIKE '% add %'
You might make your life easier if you use a SQLite database, even an in-memory one, instead of a multi-dimensional array.
As an alternative, create your own index if the description doesn’t change. If it does, try something like this (off the top of my head):
dim matchCounter() as integer redim matchCounter( lastRowIndex ) dim rx as new RegEx for wordIndex as integer = 0 to searchWords.Ubound dim searchWord as string = searchWords( wordIndex ) rx.SearchPattern = "\\b\\Q" + searchWord + "\\E\\b" for row as integer = 0 to lastRowIndex if matchCounter( row ) = wordIndex and rx.Search( rowData( row ) ) isa RegExMatch then matchCounter( row ) = wordIndex + 1 end if next next for row as integer = 0 to matchCounter.Ubound if matchCounter( row ) = searchWords.Ubound + 1 then // The row matches end if next
Your code made me realise why what I was doing was not getting the right results. I had fallen into the trap of looping using abound(myarray)-1 which was screwing the results plus I wasn’t trimming the search word list before splitting. Your code is probably quicker as I am using instr so I will look at using yours instead. Thanks to everyone for the help.