Re: Parsing with StrTok
- Posted by "Juergen Luethje" <j.lue at gmx.de> Oct 05, 2005
- 452 views
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?