Difference between revisions of "Examples"

From GiderosMobile

(added some content)
(No difference)

Revision as of 19:44, 10 July 2019



Here are some examples from the Gideros forum. Enjoy!

CSV

Load a CSV file:

function loadCSV(file)
   local function csvsplit(line)
      local t, w, l2 = {}, nil, nil
      while #line > 0 do
         w, l2 = line:match("\"([^\"]*)\"[,\r\n]?(.*)") -- Check for quoted string
         if not w then w, line = line:match("([^,]*)[,\r\n]?(.*)") -- Non quoted 
         else line = l2 end
         if not w then break end --Nothing or issue, break
         t[#t + 1] = w
      end
      return t
   end
   local headers, csv = nil, {}
   for line in io.lines(file) do
      f = csvsplit(line)
      if not headers then -- Assume first line is headers
         headers = {} for n, v in ipairs(f) do headers[v] = n end
      else
         csv[#csv + 1] = f
      end
   end
   csv.getField = function(self, row, field) return self[row][headers[field]] end
   return csv
end
--csv=loadCSV("135-bis-bt.csv")
--print(csv:getField(4,"Value"))
--print(csv:getField(7,"Time")