lua table with variable keys

i have the following table;
program = {
[“LivingRoom”]={
[1]={
{[“time”]=“10:00”}
},
[1]={
{[“time”]=“10:00”}
}
},
[“BathRoom”]={
[1]={
{[“time”]=“10:00”}
},
[1]={
{[“time”]=“10:00”}
}
}
}

Now i want to loop through this table, but i can’t getting it work.
I also have a loop that gives me the room name;
roomname = luup.rooms[luup.devices[deviceNo].room_num]

suppose that;
roomname = “BathRoom”

i want to pick the right subset from “program”;
roomProgram = program[roomname]

but this isn’t working and when i try this it works;
roomProgram = program[“BathRoom”]

i also tried;
roomProgram = program[tostring(roomname)]

Should my idea work or what is it that i’m doing wrong?

Your code really works, but you will see null in logs when trying to display a table. Just try:

luup.log(tostring(program[roomname]))

and you see it was properly selected.

So you have to provide a full path to see your string value:

luup.log(program[roomname][1]["time"])

BTW. You don’t need another {} around [“time”]=“10:00”, and you have to correct keys for LivingRoom and BathRoom nodes (you have 1 key repeated). After corrections your program table should look like this:

local program = { ["LivingRoom"]={ [1]={ ["time"]="10:00" }, [2]={ ["time"]="10:00" } }, ["BathRoom"]={ [1]={ ["time"]="10:00" }, [2]={ ["time"]="10:00" } } }

thanx for your reply and input!

The fact is that the “Test Luup code(lua)” return only an error when i try to display this one value with luup.log.
When i uncomment that line it will run OK.

About the table “design”;
This is only a part of the table and lines. The whole table contains more “columns” on the time line, so therefore i placed in extra {}

The buttom line is when i run this line, i get a error;
luup.log(program[roomname][1][“time”])

and if i run this line, there is no error;
luup.log(program[“LivingRoom”][1][“time”])

strange…

Did you try to call:

luup.log(tostring(roomname))

before:

luup.log(program[roomname][1]["time"])

Maybe the roomname is null or a string that’s not the key of your table and this causes en error.

I would recommend installing LUA on windows where you can test your LUA code easily. Google windows and LUA.