Page 1 of 1

Reading CSV

Posted: Wed Oct 12, 2016 2:14 pm
by smith8867
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

Re: Reading CSV

Posted: Wed Oct 12, 2016 2:47 pm
by dunbarx
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

Re: Reading CSV

Posted: Wed Oct 12, 2016 5:06 pm
by smith8867
Thanks! That makes more sense.