Reading CSV

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
smith8867
Posts: 57
Joined: Tue Nov 18, 2014 5:36 pm

Reading CSV

Post by smith8867 » Wed Oct 12, 2016 2:14 pm

The code below is the start of me reading in a CSV file, the contents of the file are stored in the variable fileData however I would like to sort it a little more.
The CSV file has 3 collumns, I would like to sort the data in each collum into their own seperate arrays. I was wondering how I could accomplish this.

Code: Select all

on readFile
   answer file "Choose file..."
   if the result is not "Cancel" then 
      put it into marksFile 
   end if
   put url ("file:" &marksFile) into fileData
   
   //Sort data in the CSV
   local lineNo
   split fileData by CR
   repeat with lineNo = 1 to 50
      //Code to sort collumns into variables
   end repeat
end readFile

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Reading CSV

Post by dunbarx » Wed Oct 12, 2016 2:47 pm

Hi.

Since you start with your data in the clear, why not sort in the clear? There is little control over the order of keys and their associated elements in an array. These are associative only, and, though powerful and compact, do not lend themselves to that, er, sort of processing.

Do you see? Sort with whatever delimiters you choose, and then store into an array. Know that you can:

Code: Select all

sort yourData by sortKey1 & sortkey2 and...
But know also that when you reclaim that data from the array, if you ever do, the original sort order will almost certainly be lost.

Craig Newman

smith8867
Posts: 57
Joined: Tue Nov 18, 2014 5:36 pm

Re: Reading CSV

Post by smith8867 » Wed Oct 12, 2016 5:06 pm

Thanks! That makes more sense.

Post Reply