Re: split() function

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

On Sun, 30 Apr 2000 20:32:51 PDT, No Solution <solutionnone at HOTMAIL.COM>
wrote:

> Anybody that's programmed in perl or PHP knows that the split() function
> is extremely useful.
>
> When i first downloaded Euphoria, I was suprised that there was no
> [perl-like] split function seeing as it's main datatype can be used to
> express strings. So one day i set out to write my own split() function,
> after several bad attempts, i came to a very obvious and efficient
> function.
>
> here it is :
>
> global function split(atom char,  sequence string)
>   sequence ret
>   integer c
>   ret = {{}}
>   c = 1
>   for i = 1 to length(string) do
>     if string[i] = char then
>       ret = append(ret,{})
>       c = length(ret)
>     else
>       ret[c] &= string[i]
>     end if
>   end for
>   return ret
> end function

Howdy Ian,
    I'm afraid you've awoken my subconcious by using the word 'efficient'
there. :) Those on the list who were around on the list last year will know
all about the efficiency wars (anyone remember the string type? :) ), and
it looks like I may just have to take a stab at writing a split() function
myself... :) I just can't resist it...

function carlsplit(object splitter, sequence toSplit)
    -- This function takes two sequences and returns a sequence of items
    -- found in the second sequence between occurences of the first.

    -- e.g. carlsplit("alib", "halibowdaliby") => {"h", "owd", "y"}

    sequence words
    sequence pos

    -- Make atom into sequence for match()
    if atom(splitter) then splitter = {splitter} end if

    words = {}
    -- Find the first place to split
    pos = match(splitter, toSplit)

    -- Keep going while we keep finding split markers
    while pos do
        -- Everything before the split is a 'word'
        words = append(words, toSplit[1..pos-1])

        -- Remove the 'word' and the split marker from the input
        toSplit = toSplit[pos + length(splitter)..length(toSplit)]

        -- ...and then find the next split marker
        pos = match(splitter, toSplit)
    end while

    -- Anything left must be after the last split marker
    if length(toSplit) then
        words = append(words, toSplit)
    end if

    return words
end function

Carl - I'm just a meddler at heart :)

--
Carl R. White       - Software Tester @ CTD - Carl- at -comodo.net
Cyrek the Illogical -          Geek         - cyrek- at -bigfoot.com
         Remove hyphens before mailing s'il vous plait...

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

Search



Quick Links

User menu

Not signed in.

Misc Menu