I still don't understand tables...

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
Sarcas
Posts: 86
Joined: Wednesday 11 October 2017 8:50
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.1ß
Location: Friesland
Contact:

I still don't understand tables...

Post by Sarcas »

I've spent hours reading at lua.org, lua-users.org and numerous other sites, looked at a lot of examples here at the forum, but somehow I still don't understand tables. Perhaps it has to do with the fact that I learned myself programming some 40+ years ago when i was a kid. BASIC and Pascal and dialects. I understand tables are not the arrays I used to know, but somehow I just can't grasp it.

I want to make a table ('myTable'). The index has to be a Domoticz device ID (DzDeviceID), the trigger. When that device triggers, I need to look up a premade api/json so I can open it (onActionUrl), and more info, like the name of the trigger (triggerName).

So, in my fantasy LaLaLua-language I need to do something like

Code: Select all

myTable[DzDeviceID].onActionUrl = myPremadeUrl
myTable[DzDeviceID].triggerName= thetriggername
etc
Obviously, it doesn't work.
I tried variations of declaring the array, like:

Code: Select all

local myTable= { 
            onActionUrl= {},
            triggerName= {}
        }
But I have really no clue what I am doing.

Do I need to use

Code: Select all

insert.table
? I tried to use tables as unordered sets, because the order is irrelevant. The example doesn't use the

Code: Select all

insert.table
way.

I am so confuuuused!

S.
--

Domoticz on rPi4 - RFXCOM RFXtrx433 USB - ZW090 Z-Stick Gen5 EU - IKEA Tradfri - Philips HUE - YouLess meter - SolarEdge
User avatar
jvdz
Posts: 2328
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: I still don't understand tables...

Post by jvdz »

This is an explanation about LUA Arrays/Tables in general:
Basically you need to define a array (table) for each "level".
so taking it one step at a time to be able to do this:

Code: Select all

myTable[DzDeviceID].onActionUrl = myPremadeUrl
you need to define myTable as array:

Code: Select all

myTable = {}
This allows you to do:

Code: Select all

myTable["xyz"] = "test"    -- use this format when using a variable that contains "xyz"
-- or 
myTable.xyz = "test"      -- this needs to be an hardcoded xyz

In your case eaxch entry in the myTable array needs to be an Array of its own so you define it as such

Code: Select all

DzDeviceID = "xyz"
myTable[DzDeviceID] = {}
After that you can set it in either of these 2 ways:

Code: Select all

myTable[DzDeviceID]["onActionUrl"] = "testURL"
-- or 
myTable[DzDeviceID].onActionUrl = "testURL"
Any clearer this way? :)
Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
Sarcas
Posts: 86
Joined: Wednesday 11 October 2017 8:50
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.1ß
Location: Friesland
Contact:

Re: I still don't understand tables...

Post by Sarcas »

Thanks,

It helped me out a bit. Your examples & trial and error :) Not defining the arrays was one of the problems.
--

Domoticz on rPi4 - RFXCOM RFXtrx433 USB - ZW090 Z-Stick Gen5 EU - IKEA Tradfri - Philips HUE - YouLess meter - SolarEdge
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest