Re: [If/then and sequences...]

new topic     » topic index » view thread      » older message » newer message

Hawke' wrote:
> ----tested code
>
> function find_all(object test, sequence data)
> ----returns all the indexes the find() function
> ----matches, instead of just the first index only
> ----accepts same parameters as find()
> ----if no match found, it returns 0, as find() does
> object result
>    result = {}
>    for i = 1 to length(data) do
>       if equal(test,data[i]) then
>          result &= i
>       end if
>    end for
>    if length(result) then
>         return result
>    else return 0
>    end if
> end function
>

When I need to do something like this, I usually avoid for loops (I suppose
I've never actually tested, but using find() seems like it _should_ be
faster...especially when 'data' is pretty long):

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

I use this 'double index' method a lot, especially in parsing routines.

Matt

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu