Results 1 to 3 of 3

Thread: help to simplify code please. concatenate dynamic variables

  1. #1

    Default help to simplify code please. concatenate dynamic variables

    Hi,
    I'm trying to simplify some working code I have which connects girder to my heatmiser thermostats.
    An extract is shown below (note, variables are declared elsewhere, this is just an extract and works fine, but is only part of a very long winded way of extracting bytes of data from a datastream and putting the values in a table)

    Code:
    #
    	Self.WeekdayTime1Hour = math.hextodecimal(table.concat(transport.devices.HeatmiserLink.HOUSE.TH,"",50,50))/1
    	Self.WeekdayTime1Minute = math.hextodecimal(table.concat(transport.devices.HeatmiserLink.HOUSE.TH,"",51,51))/1
    	Self.WeekdayTime1Temp = math.hextodecimal(table.concat(transport.devices.HeatmiserLink.HOUSE.TH,"",52,52))/1
    	Self.WeekdayTime2Hour = math.hextodecimal(table.concat(transport.devices.HeatmiserLink.HOUSE.TH,"",53,53))/1
    	Self.WeekdayTime2Minute = math.hextodecimal(table.concat(transport.devices.HeatmiserLink.HOUSE.TH,"",54,54))/1
    	Self.WeekdayTime2Temp = math.hextodecimal(table.concat(transport.devices.HeatmiserLink.HOUSE.TH,"",55,55))/1
    	Self.WeekdayTime3Hour = math.hextodecimal(table.concat(transport.devices.HeatmiserLink.HOUSE.TH,"",56,56))/1
    	Self.WeekdayTime3Minute = math.hextodecimal(table.concat(transport.devices.HeatmiserLink.HOUSE.TH,"",57,57))/1
    	Self.WeekdayTime3Temp = math.hextodecimal(table.concat(transport.devices.HeatmiserLink.HOUSE.TH,"",58,58))/1
    	Self.WeekdayTime4Hour = math.hextodecimal(table.concat(transport.devices.HeatmiserLink.HOUSE.TH,"",59,59))/1
    	Self.WeekdayTime4Minute = math.hextodecimal(table.concat(transport.devices.HeatmiserLink.HOUSE.TH,"",60,60))/1
    	Self.WeekdayTime4Temp = math.hextodecimal(table.concat(transport.devices.HeatmiserLink.HOUSE.TH,"",61,61))/1
    	Self.WeekendTime1Hour = math.hextodecimal(table.concat(transport.devices.HeatmiserLink.HOUSE.TH,"",62,62))/1
    	Self.WeekendTime1Minute = math.hextodecimal(table.concat(transport.devices.HeatmiserLink.HOUSE.TH,"",63,63))/1
    	Self.WeekendTime1Temp = math.hextodecimal(table.concat(transport.devices.HeatmiserLink.HOUSE.TH,"",64,64))/1
    	Self.WeekendTime2Hour = math.hextodecimal(table.concat(transport.devices.HeatmiserLink.HOUSE.TH,"",65,65))/1
    	Self.WeekendTime2Minute = math.hextodecimal(table.concat(transport.devices.HeatmiserLink.HOUSE.TH,"",66,66))/1
    	Self.WeekendTime2Temp = math.hextodecimal(table.concat(transport.devices.HeatmiserLink.HOUSE.TH,"",67,67))/1
    	Self.WeekendTime3Hour = math.hextodecimal(table.concat(transport.devices.HeatmiserLink.HOUSE.TH,"",68,68))/1
    	Self.WeekendTime3Minute = math.hextodecimal(table.concat(transport.devices.HeatmiserLink.HOUSE.TH,"",69,69))/1
    	Self.WeekendTime3Temp = math.hextodecimal(table.concat(transport.devices.HeatmiserLink.HOUSE.TH,"",70,70))/1
    	Self.WeekendTime4Hour = math.hextodecimal(table.concat(transport.devices.HeatmiserLink.HOUSE.TH,"",71,71))/1
    	Self.WeekendTime4Minute = math.hextodecimal(table.concat(transport.devices.HeatmiserLink.HOUSE.TH,"",72,72))/1
    	Self.WeekendTime4Temp = math.hextodecimal(table.concat(transport.devices.HeatmiserLink.HOUSE.TH,"",73,73))/1
    I had hoped to loop through the various parts that make up the variable name (i.e. weekend/weekday/mon-sun etc.) then the time (1,2,3,4) and the data (hr, min, temp)
    I have done this successfully in netremote, so know the theory is right. (again, the code below is just an extract of the full file)

    Code:
    	Self.TheDay={
    	[1] = "Weekday",
    	[2] = "Weekend",
    	[3] = "Monday",
    	[4] = "Tuesday",
    	[5] = "Wednesday",
    	[6] = "Thursday",
    	[7] = "Friday",
    	[8] = "Saturday",
    	[9] = "Sunday"	
    	}
    	Self.TimeGroup={
    	[1] = "1",
    	[2] = "2",
    	[3] = "3",
    	[4] = "4"	
    	}
    	Self.TheData={
    	[1] = "Hour",
    	[2] = "Minute",
    	[3] = "Temp"
    	}	
    
    *******************
    
    	Self.DSTIndex = 50
    	for s_TheDay = 1,2,1 do
    		for s_TimeGroup = 1,4,1 do
    			for s_TheData = 1,3,1 do
    			Self.."."..(s_TheDay).."Time"..(s_TimeGroup)..(s_TheData) = math.hextodecimal(table.concat(transport.devices.HeatmiserLink.HOUSE.TH,"",Self.DSTIndex,Self.DSTIndex))/1
    			Self.DSTIndex = Self.DSTIndex + 1
    			end
    		end
    	end
    The issue is how to build up (concatenate?) the variable name before the =
    The error I get is:

    Code:
    ...ram Files\Promixis\Girder5\\luascript\compat-5.1.lua:93: error loading module `HOUSE.Thermostat' (C:\Program Files\Promixis\Girder5\luascript\HOUSE/Thermostat.lua:336: `=' expected near `..')
    where line 336 is:
    Code:
    Self.."."..(s_TheDay).."Time"..(s_TimeGroup)..(s_TheData) = math.hextodecimal(table.concat(transport.devices.HeatmiserLink.HOUSE.TH,"",Self.DSTIndex,Self.DSTIndex))/1
    I'd appreciate anyone being able to advise how to do what I'm after.

    With thanks
    Jon

  2. #2
    Join Date
    Jul 2007
    Location
    Netherlands
    Posts
    342

    Default

    Quote Originally Posted by jon1977 View Post
    Code:
    ...ram Files\Promixis\Girder5\\luascript\compat-5.1.lua:93: error loading module `HOUSE.Thermostat' (C:\Program Files\Promixis\Girder5\luascript\HOUSE/Thermostat.lua:336: `=' expected near `..')
    where line 336 is:
    Code:
    Self.."."..(s_TheDay).."Time"..(s_TimeGroup)..(s_TheData) = math.hextodecimal(table.concat(transport.devices.HeatmiserLink.HOUSE.TH,"",Self.DSTIndex,Self.DSTIndex))/1
    I think that won't work because your assigning the result of an expression (right side of '=') to the result of another expression (left side of '=')
    try something like
    Code:
    local temptable = {}
    local constructed_name = Self.."."..(s_TheDay).."Time"..(s_TimeGroup)..(s_TheData) 
    local value = math.hextodecimal(table.concat(transport.devices.HeatmiserLink.HOUSE.TH,"",Self.DSTIndex,Self.DSTIndex))/1
    -- now assign value to calculated name, in the temp table
    temptable[constructed_name] = value
    So use the block quotes [] to assign to a dynamic name in a table.
    HTH.
    Using: Win7 MCE, Girder, xPL, RFXcom, HomeEasy

    http://www.thijsschreijer.nl

  3. #3

    Default

    Thanks for that.
    I tweeked it slightly, and addressed another issue with looking up the group names and the following seems to work fine:
    Code:
    	Self.TheDay={
    	[1] = "Weekday",
    	[2] = "Weekend",
    	[3] = "Monday",
    	[4] = "Tuesday",
    	[5] = "Wednesday",
    	[6] = "Thursday",
    	[7] = "Friday",
    	[8] = "Saturday",
    	[9] = "Sunday"	
    	}
    	Self.TimeGroup={
    	[1] = "1",
    	[2] = "2",
    	[3] = "3",
    	[4] = "4"	
    	}
    	Self.TheData={
    	[1] = "Hour",
    	[2] = "Minute",
    	[3] = "Temp"
    	}	
    
    ************************
    	Self.DSTIndex = 50
    	for s_TheDay = 1,2,1 do
    		for s_TimeGroup = 1,4,1 do
    			for s_TheData = 1,3,1 do
    			local dynamic_variable = (Self.TheDay[s_TheDay]).."Time"..(Self.TimeGroup[s_TimeGroup])..(Self.TheData[s_TheData]) 
    			local dynamic_value = math.hextodecimal(table.concat(transport.devices.HeatmiserLink.HOUSE.TH,"",Self.DSTIndex,Self.DSTIndex))/1
    			Self[dynamic_variable] = dynamic_value
    			Self.DSTIndex = Self.DSTIndex + 1
    			end
    		end
    	end
    thanks again.
    Jon

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •