1. Re: split() function
- Posted by Kat <gertie at ZEBRA.NET> May 01, 2000
- 451 views
----- Original Message ----- From: "No Solution" <solutionnone at HOTMAIL.COM> To: <EUPHORIA at LISTSERV.MUOHIO.EDU> Sent: Sunday, April 30, 2000 10:32 PM Subject: split() function > Hello all . > > Anybody that's programmed in perl or PHP knows that the split() function is > extremely useful. > > When i first downloaded Euphoria, I was suprised that there was no split > function seeing as it's main datatype can be used to express strings. So one > day i set out to write my own split() function, after several bad attempts, > i came to a very obvious and efficient function. > > here it is : > > global function split(atom char, sequence string) > sequence ret > integer c > ret = {{}} > c = 1 > for i = 1 to length(string) do > if string[i] = char then > ret = append(ret,{}) > c = length(ret) > else > ret[c] &= string[i] > end if > end for > return ret > end function > > so far you can only use atoms to use a delimiters to split strings by. > > use the function like this: > > sequence s,words > > s = "hello!world" > > words = split('!',s) > > anybody who writes euphoria programs that require text parsing would find > this function extremely useful,if they haven't written it already. The strtoks.e can do this, and you can specify the delimiter, the token (aka word) you want, the number of matching tokens, the locations of the tokens you are looking for in the string (aka sentence), etc. And with getxml, you can extract html-tagged sections of web pages. David Cuny wrote some PERL string parsers too. Kat