Page 1 of 1

access to the different cols of a csv file

Posted: Sat Nov 13, 2010 6:09 pm
by fre
hi,

can anybody tell me how i can have access to the different cols of a tabsepareted csv file ?
i know how to read the file and store the lines in an array. but i can not find a way to get
a specific col (or field) of the lines.

thank you for your help.

kind regards.
fre

Re: access to the different cols of a csv file

Posted: Sun Nov 14, 2010 7:30 am
by shaosean
Your best bet is to read in the CSV file and save it to a multi-dimensional array and then loop over that..

Re: access to the different cols of a csv file

Posted: Sun Nov 14, 2010 11:34 am
by fre
thanks shaosean, that is exactly what i want to do.
the problem is: i do not know how to.
what i need is something like php's explode function,
because after reading the csv file i have lines that are strings
and every string has data in it, separated by tabs.

i find it hard to find the code i need in the LiveCode dictionary.
there are hundreds of commands and functions, do i have to study the whole dic to find what i need?
what i miss, is that this dic does not tell me e.g. all the string functions, all the array function, ...

does anybody know if there is something like the php explode function in LiveCode ?

thank you and best regards.
fre.

Re: access to the different cols of a csv file

Posted: Sun Nov 14, 2010 12:06 pm
by Mark
Hi Fre,

This script might be useful to convert your CSV into into tab-delimited data. After running this script, you can do the following...

Code: Select all

// Assume that myData contains all data.
// For the occasion, assume that gDataArray is a global variable.
// line 1 contains column titles
// first line of each line is a label
set the itemDel to tab
put line 1 of myData into myTitles
// item 1 should be empty or irrelevant
delete item 1 of myTitles
delete line 1 of myData
put 1 into myItemCounter
repeat for each item in myTitles
  add 1 to myItemCounter
  put 0 into myLineCounter
  repeat for each line in myData
    add 1 to myLineCounter
    put item myItemCounter of line myLineCounter into gDataArray[item myItemCounter of myTitles][item 1 of line myLineCounter of myData]
  end repeat
end repeat
(Mind typos and line wraps; this script is untested).
First read your file into a variable, then execute the function that's on my blog and finally execute above part of the script.

Best regards,

Mark

Re: access to the different cols of a csv file

Posted: Mon Nov 15, 2010 6:55 am
by shaosean
There is the split command, not certain if it handles multi-dimensional arrays though..

Re: access to the different cols of a csv file

Posted: Mon Nov 15, 2010 11:01 am
by Mark
Hi Shaosean,

Unfortunately, the split command doesn't help in this case.

Kind regards,

Mark

Re: access to the different cols of a csv file

Posted: Mon Nov 15, 2010 7:54 pm
by fre
thank you for your help !

best regards
fre