Page 1 of 1

Array of strings and their indexing in lua

Posted: Thursday 20 October 2016 22:50
by flabrosse
All,

I'm rather new to lua, but not too programming.

To loop through heating zones in trying to put their names in an array that I can then iterate over to do something for each of them. Not knowing the syntax I read lua.org to try to learn the language.

So i try something like:

Code: Select all

local names={"kitchen", "bedroom"}
and tried to index it in a for loop. I at that point realised that table.getn(names) does not work. Also names[0] or names[1] are nil.

What am I missing?

Is there a better reference for lua as interpreted in domoticz?

Cheers

Fred

Re: Array of strings and their indexing in lua

Posted: Thursday 20 October 2016 23:00
by Egregius
You can also use php for your scripts.
I use several arrays there to calculate stuff for each room.

Re: Array of strings and their indexing in lua

Posted: Thursday 20 October 2016 23:28
by jvdz
This works fine:

Code: Select all

local names = {"kitchen", "bedroom"}
print(names[1])
print(names[2])
Jos

Re: Array of strings and their indexing in lua

Posted: Friday 21 October 2016 11:09
by flabrosse
I does indeed! I think I missed it because I started reading from index 0, not being sure what the first index would be, and the script failed (trying to create a string from nil, never reaching index 1.

Thanks for that!

Fred