On 16-Apr-02, Richard Coffre wrote:

> That's just I want to avoid but thanks because it's always useful to
> have several solutions.

Indeed - so here's another one that uses parsing instead of loops...

split: func [
    "Copy sub-strings of a set length from a string."
    str [string!] "String to be split."
    num [integer!] "Length of sub-strings."
    /local c-set blk s
][
    c-set: charset [#"^(00)" - #"^(ff)"]
    blk: copy []
    parse/all str [some [
        s: 1 num c-set (insert tail blk copy/part s num)
    ]]
    blk
]

>> split "abcdefghijklmnopqrstuvwxyz" 5
== ["abcde" "fghij" "klmno" "pqrst" "uvwxy" "z"]

I tried to cut out the need for the charset in the following function
but it ends up in an infinate loop when it starts comparing an empty
string with an empty string.  Can anyone think of a rule that would
override that?  Be interesting to know if this would be faster than
the above.  (If it worked...)

split2: func [
    "This don't work..."
    str [string!]
    num [integer!]
    /local blk s
][
    blk: copy []
    parse/all str [some[
        s: (s: copy/part s num) s (insert tail blk s)
    ]]
    blk
]

> ----Message d'origine----
>> Date: Mon, 15 Apr 2002 08:27:36 -0500
>> De: Joel Neely <[EMAIL PROTECTED]>
>> Sujet: [REBOL] Re: Tip for splitting very long string ?
>> A: [EMAIL PROTECTED]
>> 
>> Hi, Richard,
>> 
>> COFFRE Richard FTO wrote:
>>> 
>>> Hi Rebol fellows,
>>> 
>>> Is there a tip to quickly split a very long string more than
>>> 2000 characters into n characters sub strings and to create
>>> a list with these subsets ?
>>> For instance :
>>> 
>> ...
>>> 
>>> I want to split this string into substrings of 5 characters...
>>> 
>> 
>> Here's a quick-and-dirty solution:
>> 
>>    >> longstring: "abcdefghijklmnopqrstuvwxyz"
>>    == "abcdefghijklmnopqrstuvwxyz"
>>    >> i: 1
>>    == 1
>>    >> stringblock: []
>>    == []
>>    >> while [i <= length? longstring] [
>>    [    append stringblock copy/part at longstring i 5
>>    [    i: i + 5
>>    [    ]
>>    == 31
>>    >> stringblock
>>    == ["abcde" "fghij" "klmno" "pqrst" "uvwxy" "z"]
>>    >>
>> 
>> -jn-

-- 
Carl Read

-- 
To unsubscribe from this list, please send an email to
[EMAIL PROTECTED] with "unsubscribe" in the 
subject, without the quotes.

Reply via email to