Array of strings and their indexing in lua

Moderator: leecollings

Post Reply
flabrosse
Posts: 18
Joined: Thursday 07 July 2016 20:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Array of strings and their indexing in lua

Post 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
User avatar
Egregius
Posts: 2582
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Array of strings and their indexing in lua

Post by Egregius »

You can also use php for your scripts.
I use several arrays there to calculate stuff for each room.
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Array of strings and their indexing in lua

Post by jvdz »

This works fine:

Code: Select all

local names = {"kitchen", "bedroom"}
print(names[1])
print(names[2])
Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
flabrosse
Posts: 18
Joined: Thursday 07 July 2016 20:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Array of strings and their indexing in lua

Post 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
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests