|
Pages: [1]
|
 |
|
Author
|
Topic: Creating a CSV file (Read 325 times)
|
|
glennmlewis
|
I've got a 2-dimensional array of floats that I would like to turn into a CSV file. However, "form" adds spaces in between the floats, and I want to put a comma and a space between the floats (or just a comma). Why doesn't the "form" command have a refinement for specifying the delimiter to put between the items in the series (instead of a space)? How do I get around this? Must I use "form" and then do a: replace/all line " " "," That seems quite inefficient when I should have been able to build it correctly in the first place. Ideas? Thanks! -- Glenn Lewis
|
|
|
|
|
Logged
|
|
|
|
|
rebolek
|
Don't know about 'form, you can write it to RAMBO probably. There was simmilar discussion on AltME recently ( see http://www.rebol.net/altweb/rebol3/chat453.html ) and result of this discussion are these two functions that will probably help you: conjoin: func [ "Join the values in a block together with a delimiter." data [any-block!] "The values to join" delimiter "The value to put in between the above values" /only "Inserts a series delimiter as a series." /quoted "Puts string values in quotes." /local ] [ if empty? data [return make data 0] local: tail either series? local: first data [copy local] [form :local] while [not empty? data: next data] either any-string? local [ either quoted [ local: insert tail insert head local {"} {"} [local: insert insert insert insert local (delimiter) {"} first data {"}] ] [[local: insert insert local (delimiter) first data]] ] [ either only [ [local: insert insert/only local (delimiter) first data] ] [[local: insert insert local (delimiter) first data]] ] head local ]
;;-----
delimit: func [ "Put a value between the values in a series." data [series!] "The series to delimit" delimiter "The value to put into the series" /only "Inserts a series delimiter as a series." /copy "Change a copy of the series instead." /local ] [ while either copy [ if empty? data [return make data 0] local: make data 2 * length? data [ local: insert/only local first data not empty? data: next data ] ] [ local: data [not empty? local: next local] ] either only [ [local: insert/only local delimiter] ] [[local: insert local delimiter]] head local ]
|
|
|
|
|
Logged
|
|
|
|
|
Anton
|
And here's how you might use CONJOIN:
ar: [[1.0 2.0 3.0][4.0 5.0 6.0]] ; two dimensional array of floats csv: copy "" foreach blk ar [repend csv [conjoin blk ", " newline]] print csv 1.0, 2.0, 3.0 4.0, 5.0, 6.0
|
|
|
|
|
Logged
|
|
|
|
|
|
Pages: [1]
|
|
|
 |