Page 1 of 1

:SearchArrayForString InputArray ResultsTuples SearchText1 SearchText2 SearchTextN , basic search text in an array

Posted: 02 Oct 2023 03:24
by shodan
I made this simple function, to search for text in an array

It will match partial results
You can search multiple terms in one search
Search terms can be, string values, string byref and array of strings byref
All search terms from all sources are agglomerated, and then each term in the search list is scanned for in the input array
All matches are appended to an array if it already exists

Suppose you have this array

Code: Select all

set "testArray[0]=apple"
set "testArray[1]=banana"
set "testArray[2]=cherry"
set "testArray[3]=banana apple"
set "testArray[4]=grape apple"
set "testArray.lbound=0"
set "testArray.ubound=4"
and you search it with

Code: Select all

call :SearchArrayForString testArray searchResults apple banana
This DEMO code (by chatgpt)

Code: Select all

:LoopResults
if defined searchResults[%index%] (
    echo !searchResults[%index%]!
    set /a "index+=1"
    goto :LoopResults
)
Will return the following, as expected in this simple example

Code: Select all

apple
banana apple
grape apple
banana
banana apple
It gives you both the values, and the index number in the .index
The output result array are not true "tuples" however, I found it cleaner to only put the index in .index

Code: Select all

D:\dev\work>set se
searchResults=0
searchResults.lbound=0
searchResults.ubound=4
searchResults[0]=apple
searchResults[0].index=0
searchResults[1]=banana apple
searchResults[1].index=3
searchResults[2]=grape apple
searchResults[2].index=4
searchResults[3]=banana
searchResults[3].index=1
searchResults[4]=banana apple
searchResults[4].index=3

TODO
Make a version, or parameter, that only returns exact matches
This function could be faster
Search could be more parametrable

Here is the DEMO file
SearchArrayForString.zip
(1.34 KiB) Downloaded 1184 times