Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] Re: Tip for splitting very long string ?

From: carl:cybercraft at: 17-Apr-2002 19:36

On 17-Apr-02, Ingo Hohmann wrote:
> Hi Carl, > Am Die, 2002-04-16 um 13.16 schrieb Carl Read: > <..> >> 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 >> ] > <..> > you could do it like this: > split2: func [ > "This don't work... Now it does ;-)" > str [string!] > num [integer!] > /local blk s > ][ > blk: copy [] > parse/all str [some[ > s: (s: copy/part s num insert tail blk s) num skip > ]] > blk > ] > just 'skip the right number of characters. > The 'insert is in the first paren!, because otherwise the last > charecters would be added if the string does not contain a multiple > of num characters.
Very nice Ingo! And I now realise skip needs its numbers preceeding it, not following it. No wonder I'd never been able to get it to work properly before. (: So, here's the final version of the function (perhaps:)... 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 blk s ][ blk: copy [] parse/all str [some[ s: 1 num skip (insert tail blk copy/part s num) ]] blk ] Note I moved the skip to before the paren and gave it a range with a low value of 1. This was because your version would add an empty string to the block when the string could be divided evenly. ie...
>> split2 "abcde" 5
== ["abcde" ""] I reduced the contents of the paren a bit too, there being some redundancy there. On my system this function is a bit faster than my charset version and about 30% faster than the looping version posted by Anton, so thumbs up for parsing in this case. -- Carl Read