access to the different cols of a csv file

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
fre
Posts: 41
Joined: Fri Nov 12, 2010 7:22 pm

access to the different cols of a csv file

Post by fre » Sat Nov 13, 2010 6:09 pm

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

shaosean
Posts: 906
Joined: Thu Nov 04, 2010 7:53 am

Re: access to the different cols of a csv file

Post by shaosean » Sun Nov 14, 2010 7:30 am

Your best bet is to read in the CSV file and save it to a multi-dimensional array and then loop over that..

fre
Posts: 41
Joined: Fri Nov 12, 2010 7:22 pm

Re: access to the different cols of a csv file

Post by fre » Sun Nov 14, 2010 11:34 am

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.

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: access to the different cols of a csv file

Post by Mark » Sun Nov 14, 2010 12:06 pm

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
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

shaosean
Posts: 906
Joined: Thu Nov 04, 2010 7:53 am

Re: access to the different cols of a csv file

Post by shaosean » Mon Nov 15, 2010 6:55 am

There is the split command, not certain if it handles multi-dimensional arrays though..

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: access to the different cols of a csv file

Post by Mark » Mon Nov 15, 2010 11:01 am

Hi Shaosean,

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

Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

fre
Posts: 41
Joined: Fri Nov 12, 2010 7:22 pm

Re: access to the different cols of a csv file

Post by fre » Mon Nov 15, 2010 7:54 pm

thank you for your help !

best regards
fre

Post Reply