1. split() function (like perl's)

Hi everybody,

what would be the best way to write something similar to perl's split()
function (same basic functionality)?

all I want to do is be able to take a sequence/string and split() it
using a delimiting character (in this case a space).

Something like this:

seq1 = split(' ', "mary had a little lamb")
-- the results would be {"mary", "had", "a", "little", "lamb"}

thanks!

----->Buddy
budmeister1 at juno.com

_____________________________________________________________________
You don't need to buy Internet access to use free Internet e-mail.
Get completely free e-mail from Juno at http://www.juno.com
Or call Juno at (800) 654-JUNO [654-5866]

new topic     » topic index » view message » categorize

2. Re: split() function (like perl's)

>Something like this:
>
>seq1 = split(' ', "mary had a little lamb")
>-- the results would be {"mary", "had", "a", "little", "lamb"}


Haven't tested it, but try this:

function split (object x, sequence s)
sequence ret
integer pos, prev

    ret = {}
    prev = 1
    pos = find (x,s)
    while pos do
        ret = append(ret, s[prev..pos])
        prev = pos + 1
        pos = find(x, s[prev..length(s)])
    end while

    return trim(append(ret, s[prev..length(s)]))
end function

-- Not the fastest solution though..
-- It also works for things like: split ({1,2}, { 342, 234, {1,2}, 34 })

Ralf Nieuwenhuijsen
nieuwen at xs4all.nl

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu