Parse question
[1/5] from: tim:johnsons-web at: 25-Jul-2003 17:10
I have the following code:
str2blk: func[str[string!] delimiters[string! char!]
/local non-delims blk txt delims] [
if char? delimiters[delimiters: to-string delimiters]
delims: charset delimiters
non-delims: complement delims
blk: copy []
parse/all str[some [copy txt some non-delims (insert tail blk txt) | delims]]
return blk
];end function --------------------
>> str2blk "bcadefafg" "a"
== ["bc" "def" "fg"] ; that's good
; but where
>> str2blk "bcadeaafg" "a"
== ["bc" "de" "fg"] ; not good, I'd like
; to see
== ["bc" "de" "" "fg"]
IOWs, where there are two consecutive delimiters,
I would like an empty string inserted.
any Ideas?
Thanks
tim
--
Tim Johnson <[tim--johnsons-web--com]>
http://www.alaska-internet-solutions.com
http://www.johnsons-web.com
[2/5] from: AJMartin::orcon::net::nz at: 26-Jul-2003 14:39
Tim wrote:
> if char? delimiters[delimiters: to-string delimiters]
> delims: charset delimiters
One little improvement is to use this improved 'charset function
>> source charset
charset: func [
"Makes a bitset of chars for the parse function."
Chars [string! block! char!]
][
make bitset! Chars
]
and write:
delims: charset delimiters
> IOWs, where there are two consecutive delimiters,
> I would like an empty string inserted.
I couldn't figure this one out at the moment -- I blame my flu!
Andrew J Martin
Taking it easy...
ICQ: 26227169 http://www.rebol.it/Valley/ http://Valley.150m.com/
[3/5] from: rotenca:telvia:it at: 26-Jul-2003 12:53
> I have the following code:
> str2blk: func[str[string!] delimiters[string! char!]
<<quoted lines omitted: 4>>
> blk: copy []
> parse/all str[some [copy txt some non-delims (insert tail blk txt) |
delims]]
> return blk
> ];end function --------------------
<<quoted lines omitted: 8>>
> I would like an empty string inserted.
> any Ideas?
why not?
parse/all str to-string delimiters
---
Ciao
Romano
[4/5] from: tim:johnsons-web at: 26-Jul-2003 8:13
* Romano Paolo Tenca <[rotenca--telvia--it]> [030726 03:07]:
> > I have the following code:
> > str2blk: func[str[string!] delimiters[string! char!]
<<quoted lines omitted: 21>>
> why not?
> parse/all str to-string delimiters
Of course! Why didn't I think of that?
I've had this code for several versions...
Thanks Romano.
~tj~
> ---
> Ciao
> Romano
>
> --
> To unsubscribe from this list, just send an email to
> [rebol-request--rebol--com] with unsubscribe as the subject.
--
Tim Johnson <[tim--johnsons-web--com]>
http://www.alaska-internet-solutions.com
http://www.johnsons-web.com
[5/5] from: andreas:bolka:gmx at: 26-Jul-2003 18:38
Saturday, July 26, 2003, 6:13:18 PM, Tim wrote:
>>> IOWs, where there are two consecutive delimiters, I would like an
>>> empty string inserted.
>> parse/all str to-string delimiters
> Of course! Why didn't I think of that? I've had this code for
> several versions... Thanks Romano.
and once you need multi-char delimiters, you could use
split: func [
{Splits a string delimited by delim into a list of delimited
sub-strings.}
string [any-string!]
delim [any-string!]
/local tokens len pos
] [
tokens: make block! 32
len: length? delim
while [ pos: find string delim ] [
append tokens copy/part string pos
string: skip pos len
]
append tokens copy string
]
>> split "bcadeaafg" "a"
== ["bc" "de" "" "fg"]
>> split "bcadeaafg" "aa"
== ["bcade" "fg"]
--
Best regards,
Andreas
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted