RE: Fast "locate" function
- Posted by Matthew Lewis <matthewwalkerlewis at YAHOO.COM> Dec 10, 2001
- 371 views
> -----Original Message----- > From: euman at bellsouth.net [mailto:euman at bellsouth.net] > This is the best Ive seen in a while.... Thanks. :) > function find_all_2(object test, sequence data) > integer ix, jx, len > sequence result > result = {} > ix = 1 > len = length(data) > jx = find( test, data[ix..len] ) > while jx do > result &= ix+jx-1 > ix += jx > jx = find( test, data[ix..len] ) > end while > return result > end function However, when I wrote this, I knew I was looking for one-element items, not a sequence (as you seem to be interested in). You could easily convert to find multiple occurences of your data: function find_all_3(object test, sequence data) integer ix, jx, len, t_len -- add t_len for the length of your test object sequence result result = {} -- add this to get the length of the test object if atom(test) then t_len = 1 else t_len = length( test ) end if ix = 1 len = length(data) jx = match( test, data[ix..len] ) -- change find() to match() while jx do result &= ix+jx-1 ix += jx + t_len -- add the length of the test object to -- continue searching past the end of -- the last match jx = match( test, data[ix..len] ) -- change find() to match() end while return result end function The function returns a sequence of indices pointing to the first elements of the starts of your test objects within the sequence. I've found myself using this 'double' indexing a lot when using sequences. Matt Lewis