Re: Optimizing multiple searches
- Posted by petelomax Jun 28, 2017
- 1321 views
Not impressed when looking at the manual...
http://openeuphoria.org/docs/std_search.html#_2480_match_all said...
public function match_all(sequence needle, sequence haystack, integer start = 1)
Match all items of haystack in needle.
Is it me or is that just completely backwards? It should perhaps say:
Locate all instances(/subsequences) of needle in haystack.
The example from the above link may clarify my wording change for the manual, but also shows that match_all() is not at all what is need for the task as asked for above:
s = match_all("the", "the dog chased the cat under the table.") -- s is {1,16,30}
Anyway, going back to the original question, here's my suggestion
sequence lines = { "this is first line", "second line", "another line", "and this is another line- will be fourth", "and last line" } sequence terms = {"line", "will", "this"} function match_alls(sequence terms, sequence lines) integer k = 0 for i=1 to length(lines) do for j=1 to length(terms) do if match(terms[j],lines[i])=0 then exit end if if j=length(terms) then k += 1 lines[k] = lines[i] end if end for end for return lines[1..k] end function ?match_alls(terms,lines)
Output
{"and this is another line- will be fourth"}(you may want to use display() instead of ?)
To get Spock's suggestion to work as I thought it should, and match my output, I had to change
if match_any(terms, lines[i]) = length(terms) then
to
if length(match_any(terms, lines[i])) = length(terms) then
Pete
PS: Spock asked some questions that need answering (I would have kept schtum were it not for the manual thing)