Separate CSV columns into files

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
t-rex22
Posts: 20
Joined: Tue Mar 31, 2009 3:56 pm

Separate CSV columns into files

Post by t-rex22 » Tue Mar 31, 2009 7:17 pm

I have files of comma separated data with columns: time, velocity, acceleration, and displacement (for example). I want to write files that combine time with each of these columns (i.e., time and velocity, time and acceleration, etc...).

I cannot figure out how to call each column and then write only the columns I want to a file. Preferably, all files (some files have more than four columns, but the first column is always "time") would be written within the initial file selection mouseUp.

It seems like it should be easy, but I just haven't got there yet. Any help/direction would be greatly appreciated!!

Thanks!

:cry:

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4163
Joined: Sun Jan 07, 2007 9:12 pm

Post by bn » Tue Mar 31, 2009 7:53 pm

Welcome t-rex22,
try this

Code: Select all

on mouseUp
    -- answer file "please open csv file" 
    -- if it is empty then exit mouseUp
    -- put it into theFile
    -- put url ("file:" & theFile) into tAllCSV 
    -- -- you can uncomment abov code or
    --  -- load your data into a variable, tAllSCV is used in this example
    put empty into tTimeVelo
    put empty into tTimeAccel
    put empty into tTimeDisplace
    repeat for each line aLine in tAllCSV
        put item 1 of aLine & comma & item 2 of aLine & return after tTimeVelo
        put item 1 of aLine & comma & item 3 of aLine & return after tTimeAccel
        put item 1 of aLine & comma & item 4 of aLine & return after tTimeDisplace
    end repeat
    -- delete trailing return
    delete last char of tTimeVelo
    delete last char of tTimeAccel
    delete last char of tTimeDisplace
    -- now write the tTimexxx variables to a 3 files
end mouseUp
it gives you the variables with again comma separated values.
regards
Bernd

t-rex22
Posts: 20
Joined: Tue Mar 31, 2009 3:56 pm

Post by t-rex22 » Wed Apr 01, 2009 4:59 pm

Thanks Bernd, that was very helpful!!

Post Reply