1. split function
- Posted by Ted Fines <fines at macalester.edu> Sep 17, 2003
- 452 views
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
2. Re: split function
- Posted by "C. K. Lester" <cklester at yahoo.com> Sep 17, 2003
- 444 views
> I needed and wrote a split function, like the Perl split function. Ted, check out kat's string token library... http://www.pell.net/warning/ai/strtok-v2.zip. It does everything plus some. I don't know if it will meet your needs, but at least you won't have to reinvent the wheel or resort to cliches.
3. Re: split function
- Posted by Ted Fines <fines at macalester.edu> Sep 17, 2003
- 444 views
Thanks. Well, guess I already reinvented the wheel. But my wheel isn't quite as round as Kat's, from the sound of it. Oh well, got a chance to work on my carpal tunnel syndrome, anyway... --On Wednesday, September 17, 2003 10:04 AM -0500 "C. K. Lester" <cklester at yahoo.com> wrote: > > >> I needed and wrote a split function, like the Perl split function. > > Ted, check out kat's string token library... > http://www.pell.net/warning/ai/strtok-v2.zip. It does everything plus > some. I don't know if it will meet your needs, but at least you won't > have to reinvent the wheel or resort to cliches. > > > > TOPICA - Start your own email discussion group. FREE! > >
4. Re: split function
- Posted by "C. K. Lester" <cklester at yahoo.com> Sep 17, 2003
- 482 views
> Ted, check out kat's string token library... > http://www.pell.net/warning/ai/strtok-v2.zip. It does everything plus some. > I don't know if it will meet your needs, but at least you won't have to > reinvent the wheel or resort to cliches. That link doesn't work, but I did find it on her site, so go there and check it out. :)