Re: DLLs, a better split()

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

Buddy Hyllberg wrote:
> Also, I'm writing a simple split function; does anyone have a better way?

try this:

-----------begin split.ex
   --the safe, tested, verbose, and error checking version
function split1(atom lookfor, sequence data)
   --makes the sequence data into 2 sequences, at the point
   --lookfor.  the resulting pair of sequences are such that
   --the first part is up to and including the split point
   --lookfor, and the last half of the result begins just
   --after the lookfor point and continues to the end of data.
integer where,len
   len = length(data)
   if len < 2 then
        puts(1,"Error in function split: data=invalid sequence.\n")
        return data
   end if
   where = find(lookfor, data)
   if not where then
        puts(1,"Error in function split: lookfor not found.\n")
        return data
   end if
   --need this, where+1 might be past end of data
   if where=len then
        return {data,{}}
   else return {data[1..where],data[where+1..len]}
   end if
end function

   --the stripped, no error check, fast, code only version
function split2(atom lookfor, sequence data)
integer where
   where = find(lookfor, data)
   return {data[1..where],data[where+1..length(data)]}
end function


   --main
sequence mary
mary="mary had a little lamb; whose fleece was white as snow."
puts(1,mary & "\n" )
mary=split1(';',mary)
puts(1,mary[1] & "\n" & mary[2] & "\n" )

-------------------end split.ex

i put split2 in there so you could see the actual algorithm...
it could actually be made into 1 line, but it would be slower
since you would have to call find() twice.
hope this helps...
--Hawke'

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

Search



Quick Links

User menu

Not signed in.

Misc Menu