Re: DLLs, a better split()
- Posted by jiri babor <jbabor at PARADISE.NET.NZ> Sep 20, 1998
- 616 views
Buddy Hyllberg wrote: >Also, I'm writing a simple split function; does anyone have a better way? > >-- a simple split >function split( atom a, sequence b ) > sequence x, buf buf = {} x = {} > for i = 1 to length( b ) do > if compare( a, b[i] ) != 0 then > buf = buf & b[i] > else > x = append( x, buf ) > buf = {} > end if > end for > return x >end function Yes, Buddy, I have a couple of routines in my toolbox that are both better and faster than yours: function split(integer c, sequence s) -- split string s using c as delimiter sequence os integer i os={} i=find(c,s) while i do if i>1 then os=append(os,s[1..i-1]) end if s=s[i+1..length(s)] i=find(c,s) end while if length(s) then return append(os,s) end if return os end function function words(sequence c, sequence s) -- split string s using *every* element of c as delimiter sequence os integer i,j,inword os={} inword=0 j=1 while j<=length(s) do if inword then if find(s[j],c) then os=append(os,s[i..j-1]) inword=0 end if elsif not find(s[j],c) then i=j inword=1 end if j=j+1 end while if inword then os=append(os,s[i..j-1]) end if return os end function The problem with your function is that leading as well as trailing occurrences of the delimiter show up as empty strings each, and so do multiple occurrences of it inside the string. Now, I cannot even remember whether I wrote the above functions myself, or just stole them from someone else (but I usually make a note of all stolen bits) - would the rightful owner put up his/her hand?! jiri ps I leave the Windows (dll) query to the masochists on the list.