split function

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

Hi all,

I needed and wrote a split function, like the Perl split function.  It 
doesn't offer regex pattern matching, like the Perl split.  I was going to 
post it to the archive, but wanted to submit it here for review first.  The 
code I'll be submitting is just the function 'split below'.  All of the 
rest of it is just to make reviewing and testing it easier.  Please post 
any bugs with it here, I'll fix it, then submit it to the archive.

Usage:
object result
result = split(",","word1,word2,word3,etc") -- case insensitive
result = split(",","word1,word2,word3,etc",1) -- any val as 3rd arg makes 
it case-sensitive
result = split("puppy","kittenpuppyduckpuppydogpuppygorillapuppyfish")
result = split("PUPPY","kittenpuppyduckpuppydogpuppygorillapuppyfish",1)

Thanks,
Ted

----cut here----
include get.e
include wildcard.e

--
-- empty values are returned too, e.g. given{".","...something..."} returns
-- {{},{},{},{something},{},{},{}} because the nothing in front of
-- the first "." and after the last "." is an empty sequence

    sequence d,s,t,u,result,origs

    if length(myargs) > 3 or length(myargs) < 2 then
        return -1
    end if
    d = myargs[1]
    s = myargs[2]
    if length(myargs) = 3 then
        ignorecase=0
    else
        ignorecase=1
    end if

    -- handle special cases...

    -- d or s is empty
    lengthofd=length(d)
    if lengthofd = 0 or length(s) = 0 then
        return s
    end if

    -- d and s are the same
    if ignorecase=1 then
        if equal(lower(s),lower(d)) then
            return {{},{}}
        end if
    elsif equal(s,d) then
        return {{},{}}
    end if


    result = {}
    origs=s
    while 1 do
        if ignorecase then
            i = match(lower(d),lower(s))
        else
            i = match(d,s)
        end if
        if i > 0 then
            if (i+lengthofd) <= length(s) then
                t = s[i+lengthofd..length(s)]
            elsif i+lengthofd-1 = length(s) then
                result = append(result,s[1..i-1])
                exit
            end if
            if i!= 1 then
                u = s[1..i-1]
                result = append(result,u)
            else
                result = append(result,{})
            end if
            s = t
        else
            result = append(result,s)
            exit
        end if
    end while
    -- account for last portion of s=d
    if ignorecase then
        if lengthofd = 1 then
            if match(lower(d),lower(sprintf("%s",{origs[length(origs)]}))) 
then
                result=append(result,{})
            end if
        else
            if 
match(lower(d),lower(origs[length(origs)-lengthofd+1..length(origs)])) then
                result=append(result,{})
            end if
        end if
    else
        if lengthofd = 1 then
            if match(d,sprintf("%s",{origs[length(origs)]})) then
                result=append(result,{})
            end if
        else
            if match(d,origs[length(origs)-lengthofd+1..length(origs)]) then
                result=append(result,{})
            end if
        end if
    end if
    return result
end function

procedure show_results(sequence t)
    sequence d,s
    integer i
    object res

    d=t[1]
    s=t[2]
    res = split(t)
    if not atom(res) then
        if length(t) = 2 then
            puts(1,"Results of splitting {" & s & "} with {" & d & "}:\n")
        else
            puts(1,"Results of case-sensitive splitting {" & s & "} with {" 
& d & "}:\n")
        end if
        if length(res)=0 then
            puts(1,"{}\n")
        else
            for c=1 to length(res) do
                puts(1,"result[" & sprintf("%d",{c}) & "] is ")
                if length(res[c]) = 0 then
                     puts(1,"{}\n")
                else
                    puts(1, "{" & res[c] & "}\n")
                end if
            end for
        end if
    else
        puts(1,"The split function did not understand the data you gave 
it.\n")
    end if
    i = wait_key()
end procedure

sequence tests

tests = {
         {".","141.140.10.254"},
         {".","141.140.10.254."},
         {".",".141.140.10.254."},
         {"pony","141.140.65.78.0.PONY"},
         {".","."},
 
{"delimiter","field1delimiterfield2delimiterfield3delimiterfield4"},
 
{"DELIMITER","field1delimiterfield2delimiterfield3delimiterfield4",1},
 
{"delimiter","field1delimiterfield2delimiterfield3delimiterfield4delimiter"
},
 
{"delimiter","delimiterfield1delimiterfield2delimiterfield3delimiterfield4"
},
         {{},{}},
         {"pony","pony10987654321"},
         {"pony","badexample","too","many","fields"}
        }

for c = 1 to length(tests) do
    show_results(tests[c])
end for

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

Search



Quick Links

User menu

Not signed in.

Misc Menu