PDA

View Full Version : concatenate (build up) variable table names



jon1977
December 23rd, 2011, 08:39 AM
Hi,
what is the notation to build up a table name reference in LUA from some text and another variable?

I'm trying to simplify some code which polls through each of my networked thermostats one at a time and puts the received data in a common table called transport.devices.HeatmiserLink.HOUSE.TH
I have a number of thermostats, and after receiving each data reply, I currently do a set of if then's to check the receive source ID, and depending on its value, copy the common table to a table that stores all the data just for that thermostat. The common table is then cleared and used again for the next poll.
i.e. (minimised version, there are actually 10 stats
(note, self is defined earlier as HOUSE.Thermostats)


if Self.receivesource == "01" then
Self.TH1 = table.copy(transport.devices.HeatmiserLink.HOUSE.T H)
elseif Self.receivesource == "02" then
Self.TH2 = table.copy(transport.devices.HeatmiserLink.HOUSE.T H)
else
Self.THx = table.copy(transport.devices.HeatmiserLink.HOUSE.T H)
end

This works fine, but is in my mind messy.
I was hoping to use something like:


"Self.TH"..(Self.statID) = table.copy(transport.devices.HeatmiserLink.HOUSE.T H)


but this doesn't work, nor does the various options of removing brackets or speechmarks.

I've done this elsewhere to set netremote values:



NetRemote.SetVariable("HOUSE.TH"..(Self.statID)..".Settemp", Self.settemp)

with success, but there does not seem to be a version for within girder unless I'm missing it in the manual?

With thanks.
Jon

shaun5
December 23rd, 2011, 03:37 PM
I think it is:
Self("TH"..Self.statID) = table.copy(transport.devices.HeatmiserLink.HOUSE.T H)

I don't know what you are doing with the data, but if you do need to keep it: I would suggest using SQLITE (built into Lua)...

jon1977
December 23rd, 2011, 05:51 PM
Hi,
no luck with either:



Self("TH"..Self.statID) = table.copy(transport.devices.HeatmiserLink.HOUSE.T H)


or, just to be sure, I added the decimal point before the first TH:


Self(".TH"..Self.statID) = table.copy(transport.devices.HeatmiserLink.HOUSE.T H)


I get the following which is not helping me: By the way, line 333 is the line with this code in and it is the same message with or without the decimal point.



...ram Files\Promixis\Girder5\\luascript\compat-5.1.lua:93: error loading module `HOUSE.Thermostat' (C:\Program Files\Promixis\Girder5\luascript\HOUSE/Thermostat.lua:333: unexpected symbol near `=')
stack traceback:
[C]: in function `error'
...ram Files\Promixis\Girder5\\luascript\compat-5.1.lua:93: in function `loader'
...ram Files\Promixis\Girder5\\luascript\compat-5.1.lua:164: in function <...ram Files\Promixis\Girder5\\luascript\compat-5.1.lua:160>
...ram Files\Promixis\Girder5\\luascript\compat-5.1.lua:191: in function `require'
[string "JPF_01.gml:\HOUSE\Thermostats\Initialise"]:1: in main chunk


I'm currently implementing datalogging and trending using marcels link to access over odbc. Seems to be working ok.
I have no idea how to use SQLlite, although I am also using a different piece of software (media portal) for my TV server which uses it, and it does not seem as user friendly as Access (which appears to be able to do what I want).

Any other advise on getting this sorted would be appreciated!
Thanks
Jon

shaun5
December 23rd, 2011, 06:05 PM
Try []'s vs. ()'s...

myA = {};
myB = 2;
myA.TEST1 = "Hi ";
myA["TEST" .. myB] = "there.";
print (myA.TEST1 .. myA.TEST2);

prints the following in the console: Hi there.

jon1977
December 24th, 2011, 04:39 AM
well done! Thats got it.
Now for the tidy up of code to start.
Thanks again
Jon

jon1977
December 24th, 2011, 05:02 AM
I'm stuck at the very next hurdle!!
There does not seem to be a common way to build up variable references!

I previously used (which works fine, but I wanted to slim it down):


if Self.statID == 1 then
Self.TH01 = table.copy(transport.devices.HeatmiserLink.HOUSE.T H)
HOUSE.TREND.AddEntry(dt,HOUSE.TREND.ID.RoomTempTH0 1,Self.roomtemp);
elseif Self.statID == 2 then
Self.TH02 = table.copy(transport.devices.HeatmiserLink.HOUSE.T H)
HOUSE.TREND.AddEntry(dt,HOUSE.TREND.ID.RoomTempTH0 2,Self.roomtemp);
else
Self.THx = table.copy(transport.devices.HeatmiserLink.HOUSE.T H)
print ("unknown stat ID")
end


where dt is a date/time variable, HOUSE.TREND.ID.RoomTempTH01 is one of a set of entries in a table just used to cross reference an ID and Self.roomtemp is the room temperature reported by each individual stat.

to copy the table and update my link to the access database over odbc.

I've commented out the previous version and replaced it with:



Self["TH" .. Self.statID] = table.copy(transport.devices.HeatmiserLink.HOUSE.T H)
HOUSE.TREND.AddEntry(dt,HOUSE.TREND.ID[".RoomTempTH" .. Self.statID],Self.roomtemp);


The first line works fine, the second (the one adding an entry to the trend) does not.
I get:



Timer (pcall): ...ram Files\Promixis\Girder5\luascript\House/Trend.lua:331: attempt to concatenate local `ID' (a nil value)
stack traceback:
...ram Files\Promixis\Girder5\luascript\House/Trend.lua:331: in function `AddEntry'
...iles\Promixis\Girder5\luascript\HOUSE/Thermostat.lua:335: in function `handler'
...er5\luascript\Classes/DelayedExecutionDispatcher.lua:47: in function <...er5\luascript\Classes/DelayedExecutionDispatcher.lua:41>


Although line 331 isn't the one with these new commands on! That is a blank line (assuming these error reports work like that!).
If I comment out the new lines, the error dissapears, so I assume it is only to do with the new commands.

I've tried various iterations if [] and () and full stops before the roomtempTH and not, but to no avail!

I've looked extensivly in the girder and lua programming manuals and the forums.
Is there a basic rule or set of rules to follow for this, or is it a case of trial and error?

Cheers
Jon

shaun5
December 24th, 2011, 08:35 AM
I think ".RoomTempTH" should be "RoomTempTH"