Re: Parsing with StrTok

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

cklester wrote:

> Juergen Luethje wrote:

<snip>

>> What do you want "one,two,,more" to become?
>> Note the two adjacent commas.
>>      ->  { "one","two","more" }
>> or
>>      ->  { "one","two","","more" }
>
> The first one. Empty sequences aren't needed.

Maybe it can be done faster?
However, this is simple and straightforward:

-- slightly modified after code by Andy Serpa
-- (posted November 2004 on EUforum)

constant
   FALSE = 0, TRUE = not FALSE

type boolean (object x)
   if integer(x) then
      return x = FALSE or x = TRUE
   end if
   return FALSE
end type

function parse (sequence source, sequence delimList)
   -- in: source   : string to parse
   --     delimList: list of characters that are considered delimiters
   sequence parsed
   integer ptr
   boolean app

   parsed = {}
   ptr = 1
   app = FALSE
   for i = 1 to length(source) do
      if find(source[i], delimList) then
         if app = TRUE then
            parsed = append(parsed, source[ptr..i-1])
            app = FALSE
            ptr = i+1
         else
            ptr += 1
         end if
      else
         app = TRUE
      end if
   end for
   if app = TRUE then
      parsed = append(parsed, source[ptr..$])
   end if

   return parsed
end function


-- Demo
include misc.e
sequence source, delimList, parsed

source = "\rone,two\n,three,,\r\nfour,,,"
delimList = ",\r\n"
parsed = parse(source, delimList)
pretty_print(1, parsed, {3})


Regards,
   Juergen

-- 
Have you read a good program lately?

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

Search



Quick Links

User menu

Not signed in.

Misc Menu