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

[REBOL] [DELIMIT REJOIN] How to compose a string with a specific separator

From: gregg:pointillistic at: 11-Jul-2007 11:26

Hi Alessandro, Others responded already, so I'll just contribute a general purpose func to the conversation. To everyone on the list: How often do you use/need this functionality (building delimited strings)? -- Gregg delimit: func [ "Insert a delimiter between series values." series [series!] "Series to delimit. Will be modified." value "The delimiter to insert between items." /skip ;<-- be sure to use system/words/skip in this func size [integer!] "The number of items between delimiters. Default is 1." ][ ; By default, delimiters go between each item. ; MAX catches zero and negative sizes. size: max 1 any [size 1] ; If we aren't going to insert any delimiters, just return the series. ; This check means FORSKIP should always give us a series result, ; rather than NONE, so we can safely inline HEAD with it. if size + 1 > length? series [return series] ; We don't want a delimiter at the beginning. series: system/words/skip series size ; Use size+n because we're inserting a delimiter on each pass, ; and need to skip over that as well. If we're inserting a ; series into a string, we have to skip the length of that ; series. i.e. the delimiter value is more than a single item ; we need to skip. size: size + any [ all [list? series 0] ; lists behave differently; no need to skip dlm. all [any-string? series series? value length? value] all [any-string? series length? form value] 1 ] head forskip series size [insert/only series value] ] ; >> make-csv: func [block] [rejoin delimit copy block #","] ; >> make-csv ['name 'rank 'serial-number] ; == "name,rank,serial-number" ; ; >> make-parse-OR: func [block] [delimit copy block '|] ; >> make-parse-OR [yes no maybe] ; == [yes | no | maybe] ; ; >> make-name-val-lines: func [block] [form delimit/skip copy block newline 2] ; >> print make-name-val-lines ["name:" 'Gregg "rank:" 'private "serial-number:" #0000] ; name: Gregg ; rank: private ; serial-number: 0000