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

forming a string from a block but inserting a separation character

 [1/3] from: princepawn::lycos::com at: 28-Aug-2000 8:17


s: [ 9 8 7 6 5 4 3 2 1 ] How do I create the string "9.8.7.6.5.4.3.2.1" from it? I thought that reform would have a /sepchar refinement, but it didnt.

 [2/3] from: joel:neely:fedex at: 28-Aug-2000 13:05


A function to do what you want (name inspired by plywood) is
>> ply: func [s [string!] b [block!] /local r t c] [
[ r: copy "" [ t: copy "" [ foreach c b [ [ repend r [t c] [ t: s [ ] [ r [ ]
>> ply "." [9 8 7 6 5 4 3 2 1]
== "9.8.7.6.5.4.3.2.1"
>>
This is a curious omission from REBOL, as it is the obvious inverse (or near-inverse, due to typing) of parse/all "9.8.7.6.5.4.3.2.1" "." -jn- [princepawn--lycos--com] wrote:
> s: [ 9 8 7 6 5 4 3 2 1 ] > > How do I create the string "9.8.7.6.5.4.3.2.1" from it? > > I thought that reform would have a /sepchar refinement, but it didnt. > > Get your FREE Email and Voicemail at Lycos Communications at > http://comm.lycos.com
-- ; Joel Neely [joel--neely--fedex--com] 901-263-4460 38017/HKA/9677 REBOL [] print to-string debase/64 decompress #{ 789C0BCE0BAB4A7176CA48CAB53448740FABF474F3720BCC B6F4F574CFC888342AC949CE74B50500E1710C0C24000000}

 [3/3] from: brian:hawley:bigfoot at: 28-Aug-2000 20:28


[princepawn--lycos--com] wrote:
>s: [ 9 8 7 6 5 4 3 2 1 ] > >How do I create the string "9.8.7.6.5.4.3.2.1" from it?
A simple way, good for most purposes, but incorrect when the elements contain spaces: new-s: replace/all form s " " "." A faster way, and correct when the elements contain spaces: new-s: make string! 2 * length? s foreach x s [new-s: insert insert new-s x #"."] new-s: head clear back new-s This is faster mostly because all of the calls are native. Adjust the initial string length for element data type - overestimate if you have to. This method is faster yet: parse/all new-s: form s [any [to " " x: (change x #".")]] but it clobbers 'x and has same trouble with spaces in the data as the first example. Note that the [to " "] parse rule is faster than a [to #" "] rule would be, for reasons unknown. Which method you would use depends on circumstances. Usually the first method will do just fine. I like parse though... :)
>I thought that reform would have a /sepchar refinement, but it didnt.
Interesting idea. Brian Hawley