Rebol Talk Forum  |  Getting Started  |  Newbie Help  |  Topic: Creating a CSV file
Pages: [1] Print
Author Topic: Creating a CSV file  (Read 298 times)
glennmlewis
Newbie
*
Offline Offline

Posts: 6


View Profile
Creating a CSV file
« on: November 08, 2006, 09:53:19 AM »

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
Newbie
*
Offline Offline

Posts: 17


View Profile
Re: Creating a CSV file
« Reply #1 on: November 09, 2006, 01:09:50 AM »

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:

Code:
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
Jr. Member
**
Offline Offline

Posts: 65

Rebol veteran


View Profile WWW
Re: Creating a CSV file
« Reply #2 on: November 10, 2006, 10:02:11 AM »

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] Print 
Rebol Talk Forum  |  Getting Started  |  Newbie Help  |  Topic: Creating a CSV file
Jump to:  

  
Quick Search...

Advanced search
  
Welcome, Guest. Please login or register.
Did you miss your activation email?
August 30, 2008, 07:19:29 AM
Username: Password: Session Length:
  

News: 01-09-08

Alpha version of REBOL 3 has been released!


  
2235 Posts in 583 Topics by 1757 Members
Latest Member: Xzycdysz

  Rebol Talk Forum | Powered by SMF 1.0.9.
© 2001-2005, Lewis Media. All Rights Reserved.

RT design by Defiant Pc