[REBOL] Re: How to compose a string with a specific separator
From: moliad::gmail::com at: 12-Jul-2007 23:25
hi list,
here a separation (series division) func on steroids, which uses parse and
some smart indexing .
it supports, /SKIP /AT /ONLY options directly as refinements and the
separator, if its a series, will be cycled at each separation (unless you
use the /only refinement:)
SEPARATE: func [
"separates the serie into a serie of the same type using a specified
separator and many flexible options"
serie [series!] "series you wish to separate"
separator "if this is a series, it cycles each item at each separation."
/only "if separator is a series, insert it as-is at each separation."
/skip skip-count
/at offset "note, use 0 to insert at head of (before) series!"
/local here
][
skip-count: (any [skip-count 1]) - 1 + either only[length? separator]
[1]
if at [
serie: system/words/at serie offset
; add an item in serie so algorythm adds something after it, instead
of after first char
if offset = 0 [
serie: head insert serie "#"
]
]
parse/all serie [any [
here: skip
(
unless empty? next here [
either all [ series? separator not only] [
insert next here any [
; cycle through separator
all [not empty? separator first separator ]
first separator: head separator
]
separator: system/words/skip separator 1 ; error free
way to go to next item
][
insert next here separator
]
]
) skip-count skip ]
]
if offset = 0 [
; remove the first item of series, which we added
remove serie
]
head serie
]
>> separate/skip "1234567890" "." 2
== "1.23.45.67.89.0"
>> separate/skip/at "1234567890" "." 2 0
== ".12.34.56.78.90"
>>
>> separate/at/skip "1234567890" ".-" 0 2
== ".1234-5678.90"
>> separate/at/skip/only "1234567890" ".-" 0 4
== ".-1234.-5678.-90"
and a cool advanced example :-)
>> separate/at/skip "123112321233" ": " 2 2
== "12:31 12:32 12:33"
also usefull for this:
>> do separate [12 34 23 12] '+
== 81
HAVE FUN ! :-)
-MAx
On 7/12/07, Alessandro Manotti <ale870-gmail.com> wrote: