Author Topic: Lua CSV to Table  (Read 304 times)

Offline TamarackGhost

  • Newbie
  • *
  • Posts: 5
  • Karma: +0/-0
Lua CSV to Table
« on: June 23, 2018, 12:20:08 am »
The code below is a segment from my irrigation controller code. When I use Lua Test and add a few print commands, the pastPrecipTable[day] prints the value from the table, but the local precipForDayOne variable is returning a nil value. Any ideas?

The code is from here: and here:

Code: [Select]
--Convert CSV string to a lua table(
function csvToTable(csvString)
   local t={}
   local i=1

   for str in string.gmatch(csvString, "([^, ]+)") do
      t[i] = str
      i = i + 1

   return t

--Get past precipitation as CSV string
local pastPrecipCSV = luup.variable_get(RAIN_SID,"PastPrecip",RAIN_ID)

--Convert CSV string to a lua table
local pastPrecipTable = csvToTable(pastPrecipCSV)

--Number of elements in table
local tableSize = #pastPrecipTable

for day = 1, tableSize do
   --Get past precip for each day
   local precipForDayOne = pastPrecipTable[day]

for day = 2, tableSize do
   --Get past precip for each day
  local precipForDayTwo = pastPrecipTable[day]