Re: Optimizing multiple searches
- Posted by ghaberek (admin) Jun 28, 2017
- 1350 views
rneu said...
I have 2 lists (lines and terms) and I have to select out only those entries of 'lines' where all items of 'terms' are present. Above code works but can it be optimized or shortened since I see that functions such as match_all, find_all, find_each etc are also available?
Since you require all terms to match, then you can jump out of the loop of terms as soon as you don't find one you're looking for. This is easier if you invert the for loops: loop through all of the lines, scanning each one for the required terms.
include std/console.e include std/types.e 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"} sequence outlist = {} for i = 1 to length( lines ) do integer found_all = TRUE for j = 1 to length( terms ) do if not match( terms[j], lines[i] ) then found_all = FALSE break end if end for if found_all then outlist = append( outlist, lines[i] ) end if end for display( outlist )
-Greg