converting Variable into table

Moderator: leecollings

Post Reply
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

converting Variable into table

Post by emme »

Hi,

I have few time scripts that are supposed to run several times a day, at a specific time.
Actually the time is trigged inseide the LUA code, but I was looking for a smarter way to drive it...

So I was thinking to use a table inside a variable... but here I come with a problem using such information in LUA:

first: My script is supposed to run at hour 7, 11,14,17,21 (munite is fixed at 00), so I create a string global variable named RunOnTime with values 7, 11, 14, 17, 21

in LUA I don't know how to convert it into an array:

Code: Select all

tstArray = { uservariables['RunOnTime'] }
seems not working while

Code: Select all

tstArray = {7, 11, 14, 17, 21}
works correctly

how do I convert strings into tables?! :(
thanks
ciao
M
The most dangerous phrase in any language is:
"We always done this way"
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: converting Variable into table

Post by emme »

uh... found a possible solution... but I would like it would be a better and faster way....

got some code from lua-users.org

Code: Select all

function fromCSV (s)
  s = s .. ','        -- ending comma
  local t = {}        -- table to collect fields
  local fieldstart = 1
  repeat
    -- next field is quoted? (start with `"'?)
    if string.find(s, '^"', fieldstart) then
      local a, c
      local i  = fieldstart
      repeat
        -- find closing quote
        a, i, c = string.find(s, '"("?)', i+1)
      until c ~= '"'    -- quote not followed by quote?
      if not i then error('unmatched "') end
      local f = string.sub(s, fieldstart+1, i-1)
      table.insert(t, (string.gsub(f, '""', '"')))
      fieldstart = string.find(s, ',', i) + 1
    else                -- unquoted; find next comma
      local nexti = string.find(s, ',', fieldstart)
      table.insert(t, string.sub(s, fieldstart, nexti-1))
      fieldstart = nexti + 1
    end
  until fieldstart > string.len(s)
  return t
end
The most dangerous phrase in any language is:
"We always done this way"
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest