Home   Archive   Permalink



Parsing CSV: splitting strings to block items

hi, I need to work with two columns in a semicolon-separated CSV file.
I can extract the relevant columns like this:
    
lines: read %file.csv
foreach line lines [
    parse line [thru 7 copy cols to 23]
    print mold parse/all cols ";"
]
    
Output:
["181'796" "181'865"]
["181'575" "181'643"]
    
But -- the output is a long string,
so `print cols/2` gives me "8", not the entire 2nd field as I would hope.
    
How do I convert this output to block items I could work on with?
(My final goal is to subtract col2 from col1 and sum the results.)
    
Many thanks!

posted by:   martz       29-Oct-2017/17:15:28-7:00



Looks as if you've done the correct dissection but haven't stored it anywhere. COLS is still that which you copied in your PARSE rule.
    
I'd suggest using COLLECT:
    
     cols: collect [
         foreach line lines [
             parse line [thru 7 copy cols to 23]
             keep/only parse/all cols ";"
         ]
     ]

posted by:   Chris       29-Oct-2017/18:23:04-7:00



Ok, I did it like this -- probably not clean, but good enough for first script by a non-coder. :)
    
---------------
    
lines: read/lines %sp.csv
sum: func [x y] [x - y]
distances: []
    
foreach line lines [
    parse line [thru 7 copy a to 15]
    parse line [thru 15 copy b to 23]
    append distances sum (to-integer b) (to-integer a)
]
    
---------------

posted by:   martz       29-Oct-2017/18:29:35-7:00



Ok, I did it like this -- probably not very elegant, but good enough for first script by a non-coder. :)
    
-----
lines: read/lines %sp.csv
sum: func [x y] [x - y]
distances: []
    
foreach line lines [
    parse line [thru 7 copy a to 15]
    parse line [thru 15 copy b to 23]
    append distances sum (to-integer b) (to-integer a)
]
    


posted by:   martz       29-Oct-2017/18:32:07-7:00



Thanks Chris, 'collect' and 'keep/only' are more elegant indeed.

posted by:   martz       30-Oct-2017/5:57:41-7:00



I hesitate to steer anyone into my way of doing things here in Beginnerland, but recently I have had to do a lot of dissecting of CSV files that have column headings in the first line followed by lines of data. To save myself huge amounts of time, I made a couple modules for handling this specific type of file. You are welcome to them if they might help. The coding style would be considered by some to be less than elegant. I have to plod through things in order to understand them.
    
http://cobolrebol.com/pages/freestuff/csvfile.r
    
http://cobolrebol.com/pages/freestuff/csvobj.r
    
    


posted by:   Steven White       30-Oct-2017/9:39:07-7:00



thanks Steven! I will definitely take time to dig into your scripts. the comments you've added make them particularly useful for beginners like me.

posted by:   martz       2-Nov-2017/18:30:20-7:00



Name:


Message:


Type the reverse of this captcha text: "? e v i t a g e n"



Home