1. Re: split() function

Carl R. White wrote:

>[...] Those on the list who were around on the list last year will know
>all about the efficiency wars (anyone remember the string type? :) ), and
>it looks like I may just have to take a stab at writing a split() function
>myself... :) I just can't resist it...

Neither can I; I, too, remember the "string" type quite well. The following
is my own foray into "split()" territory, a function I have dubbed
"parse()". I've even gone to the trouble of writing its counterpart
function, "deparse()" (or should it be "unparse"? I never could decide...)
Oh well, enjoy!

global function parse(sequence s, object x)
-- parse s, based on delimiter x, into a sequence of values
   sequence list
   integer len, prev

   len = length(s)
   list = {}
   prev = 0

   for curr = 1 to len do
      -- we sling through the whole sequence only once,
      -- which may generally be the most efficient method
      if equal(s[curr], x) then
         -- grab the slice between the delimiters
         list = append(list, s[prev+1..curr-1])
         prev = curr
      end if
   end for

   if prev != len then
      -- the last element was not a delimiter,
      -- so we grab the remaining slice from the sequence
      list = append(list, s[prev+1..len])
   end if

   return list -- the list of delimited values
end function

global function deparse(sequence list, object x)
-- deparse list, delimiting each list element with x
   sequence s

   s = {}
   for i = 1 to length(list) do
      -- concatenate the list element to s,
      -- then append our delimiter
      s &= list[i]
      s = append(s, x)
   end for

   return s -- our deparsed sequence
end function


TTYL,
Gabriel Boehme

----------
"Today's funny innovation is tomorrow's stale formula."

Bill Watterson (he of the "Calvin and Hobbes" fame)
----------
______________________________________________________
PLEASE IGNORE ANY ADS YOU MAY SEE BELOW THIS LINE. smile
________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com

new topic     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu