It is currently April 25th, 2024, 1:54 am

Lua 'loadstring' problems

Discuss the use of Lua in Script measures.
User avatar
donutmtoazt
Posts: 50
Joined: July 10th, 2011, 9:15 am
Location: Fresno, CA

Lua 'loadstring' problems

Post by donutmtoazt »

Alright, so I've been working on this code for a while and I've been sitting on it hoping that an epiphany would strike and I would figure out the problem, but I have yet to reach that moment. I'm hoping you guys can give me some insight with your greater knowledge of Lua and Rainmeter.

Code: Select all

PROPERTIES =
{

}

function Initialize()
	--[[
		Ooh boy, that's a lot. Basically, 49 measures for the times of 49 events, and then the 98 string meters that are tied to the 49 events.	
	--]]

	E1 = SKIN:GetMeasure("mTime1")
	E2 = SKIN:GetMeasure("mTime2")
	E3 = SKIN:GetMeasure("mTime3")
	E4 = SKIN:GetMeasure("mTime4")
	E5 = SKIN:GetMeasure("mTime5")
	E6 = SKIN:GetMeasure("mTime6")
	E7 = SKIN:GetMeasure("mTime7")
	E8 = SKIN:GetMeasure("mTime8")
	E9 = SKIN:GetMeasure("mTime9")
	E10 = SKIN:GetMeasure("mTime10")
	E11 = SKIN:GetMeasure("mTime11")
	E12 = SKIN:GetMeasure("mTime12")
	E13 = SKIN:GetMeasure("mTime13")
	E14 = SKIN:GetMeasure("mTime14")
	E15 = SKIN:GetMeasure("mTime15")
	E16 = SKIN:GetMeasure("mTime16")
	E17 = SKIN:GetMeasure("mTime17")
	E18 = SKIN:GetMeasure("mTime18")
	E19 = SKIN:GetMeasure("mTime19")
	E20 = SKIN:GetMeasure("mTime20")
	E21 = SKIN:GetMeasure("mTime21")
	E22 = SKIN:GetMeasure("mTime22")
	E23 = SKIN:GetMeasure("mTime23")
	E24 = SKIN:GetMeasure("mTime24")
	E25 = SKIN:GetMeasure("mTime25")
	E26 = SKIN:GetMeasure("mTime26")
	E27 = SKIN:GetMeasure("mTime27")
	E28 = SKIN:GetMeasure("mTime28")
	E29 = SKIN:GetMeasure("mTime29")
	E30 = SKIN:GetMeasure("mTime30")
	E31 = SKIN:GetMeasure("mTime31")
	E32 = SKIN:GetMeasure("mTime32")
	E33 = SKIN:GetMeasure("mTime33")
	E34 = SKIN:GetMeasure("mTime34")
	E35 = SKIN:GetMeasure("mTime35")
	E36 = SKIN:GetMeasure("mTime36")
	E37 = SKIN:GetMeasure("mTime37")
	E38 = SKIN:GetMeasure("mTime38")
	E39 = SKIN:GetMeasure("mTime39")
	E40 = SKIN:GetMeasure("mTime40")
	E41 = SKIN:GetMeasure("mTime41")
	E42 = SKIN:GetMeasure("mTime42")
	E43 = SKIN:GetMeasure("mTime43")
	E44 = SKIN:GetMeasure("mTime44")
	E45 = SKIN:GetMeasure("mTime45")
	E46 = SKIN:GetMeasure("mTime46")
	E47 = SKIN:GetMeasure("mTime47")
	E48 = SKIN:GetMeasure("mTime48")
	E49 = SKIN:GetMeasure("mTime49")

	TL1 = SKIN:GetMeter("Item1Left")
	TL2 = SKIN:GetMeter("Item2Left")
	TL3 = SKIN:GetMeter("Item3Left")
	TL4 = SKIN:GetMeter("Item4Left")
	TL5 = SKIN:GetMeter("Item5Left")
	TL6 = SKIN:GetMeter("Item6Left")
	TL7 = SKIN:GetMeter("Item7Left")
	TL8 = SKIN:GetMeter("Item8Left")
	TL9 = SKIN:GetMeter("Item9Left")
	TL10 = SKIN:GetMeter("Item10Left")
	TL11 = SKIN:GetMeter("Item11Left")
	TL12 = SKIN:GetMeter("Item12Left")
	TL13 = SKIN:GetMeter("Item13Left")
	TL14 = SKIN:GetMeter("Item14Left")
	TL15 = SKIN:GetMeter("Item15Left")
	TL16 = SKIN:GetMeter("Item16Left")
	TL17 = SKIN:GetMeter("Item17Left")
	TL18 = SKIN:GetMeter("Item18Left")
	TL19 = SKIN:GetMeter("Item19Left")
	TL20 = SKIN:GetMeter("Item20Left")
	TL21 = SKIN:GetMeter("Item21Left")
	TL22 = SKIN:GetMeter("Item22Left")
	TL23 = SKIN:GetMeter("Item23Left")
	TL24 = SKIN:GetMeter("Item24Left")
	TL25 = SKIN:GetMeter("Item25Left")
	TL26 = SKIN:GetMeter("Item26Left")
	TL27 = SKIN:GetMeter("Item27Left")
	TL28 = SKIN:GetMeter("Item28Left")
	TL29 = SKIN:GetMeter("Item29Left")
	TL30 = SKIN:GetMeter("Item30Left")
	TL31 = SKIN:GetMeter("Item31Left")
	TL32 = SKIN:GetMeter("Item32Left")
	TL33 = SKIN:GetMeter("Item33Left")
	TL34 = SKIN:GetMeter("Item34Left")
	TL35 = SKIN:GetMeter("Item35Left")
	TL36 = SKIN:GetMeter("Item36Left")
	TL37 = SKIN:GetMeter("Item37Left")
	TL38 = SKIN:GetMeter("Item38Left")
	TL39 = SKIN:GetMeter("Item39Left")
	TL40 = SKIN:GetMeter("Item40Left")
	TL41 = SKIN:GetMeter("Item41Left")
	TL42 = SKIN:GetMeter("Item42Left")
	TL43 = SKIN:GetMeter("Item43Left")
	TL44 = SKIN:GetMeter("Item44Left")
	TL45 = SKIN:GetMeter("Item45Left")
	TL46 = SKIN:GetMeter("Item46Left")
	TL47 = SKIN:GetMeter("Item47Left")
	TL48 = SKIN:GetMeter("Item48Left")
	TL49 = SKIN:GetMeter("Item49Left")
	
	TR1 = SKIN:GetMeter("Item1Right")
	TR2 = SKIN:GetMeter("Item2Right")
	TR3 = SKIN:GetMeter("Item3Right")
	TR4 = SKIN:GetMeter("Item4Right")
	TR5 = SKIN:GetMeter("Item5Right")
	TR6 = SKIN:GetMeter("Item6Right")
	TR7 = SKIN:GetMeter("Item7Right")
	TR8 = SKIN:GetMeter("Item8Right")
	TR9 = SKIN:GetMeter("Item9Right")
	TR10 = SKIN:GetMeter("Item10Right")
	TR11 = SKIN:GetMeter("Item11Right")
	TR12 = SKIN:GetMeter("Item12Right")
	TR13 = SKIN:GetMeter("Item13Right")
	TR14 = SKIN:GetMeter("Item14Right")
	TR15 = SKIN:GetMeter("Item15Right")
	TR16 = SKIN:GetMeter("Item16Right")
	TR17 = SKIN:GetMeter("Item17Right")
	TR18 = SKIN:GetMeter("Item18Right")
	TR19 = SKIN:GetMeter("Item19Right")
	TR20 = SKIN:GetMeter("Item20Right")
	TR21 = SKIN:GetMeter("Item21Right")
	TR22 = SKIN:GetMeter("Item22Right")
	TR23 = SKIN:GetMeter("Item23Right")
	TR24 = SKIN:GetMeter("Item24Right")
	TR25 = SKIN:GetMeter("Item25Right")
	TR26 = SKIN:GetMeter("Item26Right")
	TR27 = SKIN:GetMeter("Item27Right")
	TR28 = SKIN:GetMeter("Item28Right")
	TR29 = SKIN:GetMeter("Item29Right")
	TR30 = SKIN:GetMeter("Item30Right")
	TR31 = SKIN:GetMeter("Item31Right")
	TR32 = SKIN:GetMeter("Item32Right")
	TR33 = SKIN:GetMeter("Item33Right")
	TR34 = SKIN:GetMeter("Item34Right")
	TR35 = SKIN:GetMeter("Item35Right")
	TR36 = SKIN:GetMeter("Item36Right")
	TR37 = SKIN:GetMeter("Item37Right")
	TR38 = SKIN:GetMeter("Item38Right")
	TR39 = SKIN:GetMeter("Item39Right")
	TR40 = SKIN:GetMeter("Item40Right")
	TR41 = SKIN:GetMeter("Item41Right")
	TR42 = SKIN:GetMeter("Item42Right")
	TR43 = SKIN:GetMeter("Item43Right")
	TR44 = SKIN:GetMeter("Item44Right")
	TR45 = SKIN:GetMeter("Item45Right")
	TR46 = SKIN:GetMeter("Item46Right")
	TR47 = SKIN:GetMeter("Item47Right")
	TR48 = SKIN:GetMeter("Item48Right")
	TR49 = SKIN:GetMeter("Item49Right")

	d0 = {}
	d1 = {}
	d2 = {}
	d3 = {}
	d4 = {}
	d5 = {}
	d6 = {}
	E1 = {E1,E2,E3,E4,E5,E6,E7,E8,E9,E10,E11,E12,E13,E14,E15,E16,E17,E18,E19,E20,E21,E22,E23,E24,E25,E26,E27,E28,E29,E30,E31,E32,E33,E34,E35,E36,E37,E38,E39,E40,E41,E42,E43,E44,E45,E46,E47,E48,E49}
	TL = {TL1,TL2,TL3,TL4,TL5,TL6,TL7,TL8,TL9,TL10,TL11,TL12,TL13,TL14,TL15,TL16,TL17,TL18,TL19,TL20,TL21,TL22,TL23,TL24,TL25,TL26,TL27,TL28,TL29,TL30,TL31,TL32,TL33,TL34,TL35,TL36,TL37,TL38,TL39,TL40,TL41,TL42,TL43,TL44,TL45,TL46,TL47,TL48,TL49}
	TR = {TR1,TR2,TR3,TR4,TR5,TR6,TR7,TR8,TR9,TR10,TR11,TR12,TR13,TR14,TR15,TR16,TR17,TR18,TR19,TR20,TR21,TR22,TR23,TR24,TR25,TR26,TR27,TR28,TR29,TR30,TR31,TR32,TR33,TR34,TR35,TR36,TR37,TR38,TR39,TR40,TR41,TR42,TR43,TR44,TR45,TR46,TR47,TR48,TR49}
	E = {}
	
end -- function Initialize

function Update()
	
	a = 1
	
	--[[
	This block gathers the times from the 49 events gathered by the webparser, takes each one at a time, and substitutes the text months to the equivalent
	numerical value for how many days have passed in the year. Then it substitutes the years to how many days have passed since the beginning of 2011, e.g.,
	Jan 1, 2011 and Jan 1, 2012 have the values 0 + 1 + 0 and 0 + 1 + 365, respectively. Finally, 'f = loadstring("E[a] = "..E[a])' changes the string of
	additions (^previous example^) and adds them together, creating 1 and 366.	
	--]]
	repeat
		E[a] = E1[a]:GetStringValue()
		
		if string.match(E[a], '.*(2012).*') == '2012' then
			E[a] = string.gsub(E[a], '.*(Jan) ', '000 + ', 1)
			E[a] = string.gsub(E[a], '.*(Feb) ', '031 + ', 1)
			E[a] = string.gsub(E[a], '.*(Mar) ', '060 + ', 1)
			E[a] = string.gsub(E[a], '.*(Apr) ', '091 + ', 1)
			E[a] = string.gsub(E[a], '.*(May) ', '121 + ', 1)
			E[a] = string.gsub(E[a], '.*(Jun) ', '152 + ', 1)
			E[a] = string.gsub(E[a], '.*(Jul) ', '182 + ', 1)
			E[a] = string.gsub(E[a], '.*(Aug) ', '213 + ', 1)
			E[a] = string.gsub(E[a], '.*(Sep) ', '244 + ', 1)
			E[a] = string.gsub(E[a], '.*(Oct) ', '274 + ', 1)
			E[a] = string.gsub(E[a], '.*(Nov) ', '305 + ', 1)
			E[a] = string.gsub(E[a], '.*(Dec) ', '335 + ', 1)
		elseif string.match(E[a], '.*(2016).*') == '2016' then
			E[a] = string.gsub(E[a], '.*(Jan) ', '000 + ', 1)
			E[a] = string.gsub(E[a], '.*(Feb) ', '031 + ', 1)
			E[a] = string.gsub(E[a], '.*(Mar) ', '060 + ', 1)
			E[a] = string.gsub(E[a], '.*(Apr) ', '091 + ', 1)
			E[a] = string.gsub(E[a], '.*(May) ', '121 + ', 1)
			E[a] = string.gsub(E[a], '.*(Jun) ', '152 + ', 1)
			E[a] = string.gsub(E[a], '.*(Jul) ', '182 + ', 1)
			E[a] = string.gsub(E[a], '.*(Aug) ', '213 + ', 1)
			E[a] = string.gsub(E[a], '.*(Sep) ', '244 + ', 1)
			E[a] = string.gsub(E[a], '.*(Oct) ', '274 + ', 1)
			E[a] = string.gsub(E[a], '.*(Nov) ', '305 + ', 1)
			E[a] = string.gsub(E[a], '.*(Dec) ', '335 + ', 1)
		else
			E[a] = string.gsub(E[a], '.*(Jan) ', '000 + ', 1)
			E[a] = string.gsub(E[a], '.*(Feb) ', '031 + ', 1)
			E[a] = string.gsub(E[a], '.*(Mar) ', '059 + ', 1)
			E[a] = string.gsub(E[a], '.*(Apr) ', '090 + ', 1)
			E[a] = string.gsub(E[a], '.*(May) ', '120 + ', 1)
			E[a] = string.gsub(E[a], '.*(Jun) ', '151 + ', 1)
			E[a] = string.gsub(E[a], '.*(Jul) ', '181 + ', 1)
			E[a] = string.gsub(E[a], '.*(Aug) ', '212 + ', 1)
			E[a] = string.gsub(E[a], '.*(Sep) ', '243 + ', 1)
			E[a] = string.gsub(E[a], '.*(Oct) ', '273 + ', 1)
			E[a] = string.gsub(E[a], '.*(Nov) ', '304 + ', 1)
			E[a] = string.gsub(E[a], '.*(Dec) ', '334 + ', 1)
		end
		
		E[a] = string.gsub(E[a], ', (2011).*', ' + 0000', 1)
		E[a] = string.gsub(E[a], ', (2012).*', ' + 0365', 1)
		E[a] = string.gsub(E[a], ', (2013).*', ' + 0731', 1)
		E[a] = string.gsub(E[a], ', (2014).*', ' + 1096', 1)
		E[a] = string.gsub(E[a], ', (2015).*', ' + 1462', 1)
		E[a] = string.gsub(E[a], ', (2016).*', ' + 1872', 1)
		
		f = loadstring("E[a] = "..E[a])
		f()
		
		a = a + 1
	until a == 50
	
	--[[
	Same formula from above, applied to the current date so that the event date can be compared to the current date.
	--]]
	
	curdate = os.date("%m %d %Y")
	if string.match(curdate, '2012') == '2012' then
		curdate = string.gsub(curdate, '(01) ', '000 + ', 1)
		curdate = string.gsub(curdate, '(02) ', '031 + ', 1)
		curdate = string.gsub(curdate, '(03) ', '060 + ', 1)
		curdate = string.gsub(curdate, '(04) ', '091 + ', 1)
		curdate = string.gsub(curdate, '(05) ', '121 + ', 1)
		curdate = string.gsub(curdate, '(06) ', '152 + ', 1)
		curdate = string.gsub(curdate, '(07) ', '182 + ', 1)
		curdate = string.gsub(curdate, '(08) ', '213 + ', 1)
		curdate = string.gsub(curdate, '(09) ', '244 + ', 1)
		curdate = string.gsub(curdate, '(10) ', '274 + ', 1)
		curdate = string.gsub(curdate, '(11) ', '305 + ', 1)
		curdate = string.gsub(curdate, '(12) ', '335 + ', 1)
	elseif string.match(curdate, '2016') == '2016' then
		curdate = string.gsub(curdate, '(01) ', '000 + ', 1)
		curdate = string.gsub(curdate, '(02) ', '031 + ', 1)
		curdate = string.gsub(curdate, '(03) ', '060 + ', 1)
		curdate = string.gsub(curdate, '(04) ', '091 + ', 1)
		curdate = string.gsub(curdate, '(05) ', '121 + ', 1)
		curdate = string.gsub(curdate, '(06) ', '152 + ', 1)
		curdate = string.gsub(curdate, '(07) ', '182 + ', 1)
		curdate = string.gsub(curdate, '(08) ', '213 + ', 1)
		curdate = string.gsub(curdate, '(09) ', '244 + ', 1)
		curdate = string.gsub(curdate, '(10) ', '274 + ', 1)
		curdate = string.gsub(curdate, '(11) ', '305 + ', 1)
		curdate = string.gsub(curdate, '(12) ', '335 + ', 1)
	else
		curdate = string.gsub(curdate, '(01) ', '000 + ', 1)
		curdate = string.gsub(curdate, '(02) ', '031 + ', 1)
		curdate = string.gsub(curdate, '(03) ', '059 + ', 1)
		curdate = string.gsub(curdate, '(04) ', '090 + ', 1)
		curdate = string.gsub(curdate, '(05) ', '120 + ', 1)
		curdate = string.gsub(curdate, '(06) ', '151 + ', 1)
		curdate = string.gsub(curdate, '(07) ', '181 + ', 1)
		curdate = string.gsub(curdate, '(08) ', '212 + ', 1)
		curdate = string.gsub(curdate, '(09) ', '243 + ', 1)
		curdate = string.gsub(curdate, '(10) ', '273 + ', 1)
		curdate = string.gsub(curdate, '(11) ', '304 + ', 1)
		curdate = string.gsub(curdate, '(12) ', '334 + ', 1)
	end
	
	curdate = string.gsub(curdate, ' (2011)', ' + 0000', 1)
	curdate = string.gsub(curdate, ' (2012)', ' + 0365', 1)
	curdate = string.gsub(curdate, ' (2013)', ' + 0731', 1)
	curdate = string.gsub(curdate, ' (2014)', ' + 1096', 1)
	curdate = string.gsub(curdate, ' (2015)', ' + 1462', 1)
	curdate = string.gsub(curdate, ' (2016)', ' + 1872', 1)
	f = loadstring("curdate = "..curdate)
	f()
	
	c = 1
	
	--[[
	Here's the comparison! Takes each event date, figures out how close to the current date it is, and then sorts it appropriately:
	if the event is tomorrow (one day away), the strings related to the event are sorted into the corresponding tables (d1, in this case).
	If the event is seven days or more away (anytime past a week), the strings are hidden because there is no need for them.
	--]]
	repeat
		if E[c] - curdate < 7 then
			if E[c] - curdate == 0 then
				table.insert(d0, TL[c])
				table.insert(d0, TR[c])
			elseif E[c] - curdate == 1 then
				table.insert(d1, TL[c])
				table.insert(d1, TR[c])
			elseif E[c] - curdate == 2 then
				table.insert(d2, TL[c])
				table.insert(d2, TR[c])
			elseif E[c] - curdate == 3 then
				table.insert(d3, TL[c])
				table.insert(d3, TR[c])
			elseif E[c] - curdate == 4 then
				table.insert(d4, TL[c])
				table.insert(d4, TR[c])
			elseif E[c] - curdate == 5 then
				table.insert(d5, TL[c])
				table.insert(d5, TR[c])
			else
				table.insert(d6, TL[c])
				table.insert(d6, TR[c])
			end
		else
			TL[c]:Hide()
			TR[c]:Hide()
		end
		c = c + 1
	until c == 50
	
	return E[1]
	
	--[[
	There will be more to this, but until I can get this section working correctly to hide strings that are seven days away,
	I don't really want to put more work into it.
	--]]
	
end -- function Update
So, the problem here is the end of my first block in Update, 'f = loadstring("E[a] = "..E[a])', which should load the string and replace E[a] with the number of days added up, so E[1] = 212 + 22 + 0000 would become E[1] = 234. The Rainmeter log, though, returns this: 'Script: [string "E[a] = 212 + 22 + 0000"] :1: attempt to access global 'E' (a nil value).' I would understand the issue better if I hadn't run similar types of code through the interpreter and received success, so I'm not sure where the problem lies. Any insight would be helpful.
This is my keyboard. There are many like it, but this one is mine.
User avatar
Kaelri
Developer
Posts: 1721
Joined: July 25th, 2009, 4:47 am

Re: Lua 'loadstring' problems

Post by Kaelri »

First, although unrelated to the specific problem at hand, you can make your life a lot easier by proceduralizing the first section:

Code: Select all

for i = 1, 49 do
		msMeasure = SKIN:GetMeasure("mTime"..i)
		E[i] = msMeasure:GetStringValue()
		TL[i] = SKIN:GetMeter("Item"..i.."Left")
		TR[i] = SKIN:GetMeter("Item"..i.."Right")
end
As for the loadstring issue, I think you can bypass it altogether:

Code: Select all

E[a] = tonumber(E[a])
This should convert the entire argument (e.g. "212 + 22 + 0000") into a number.

Complete script with my suggested changes:

Code: Select all

PROPERTIES =
{

}

function Initialize()
   --[[
      Ooh boy, that's a lot. Basically, 49 measures for the times of 49 events, and then the 98 string meters that are tied to the 49 events.   
   --]]
   d0 = {}
   d1 = {}
   d2 = {}
   d3 = {}
   d4 = {}
   d5 = {}
   d6 = {}
   E = {}
   TL = {}
   TR = {}
	for i = 1, 49 do
		msMeasure = SKIN:GetMeasure("mTime"..i)
		E[i] = msMeasure:GetStringValue()
		TL[i] = SKIN:GetMeter("Item"..i.."Left")
		TR[i] = SKIN:GetMeter("Item"..i.."Right")
	end
end

function Update()
   
   --[[
   This block gathers the times from the 49 events gathered by the webparser, takes each one at a time, and substitutes the text months to the equivalent
   numerical value for how many days have passed in the year. Then it substitutes the years to how many days have passed since the beginning of 2011, e.g.,
   Jan 1, 2011 and Jan 1, 2012 have the values 0 + 1 + 0 and 0 + 1 + 365, respectively. Finally, 'f = loadstring("E[a] = "..E[a])' changes the string of
   additions (^previous example^) and adds them together, creating 1 and 366.   
   --]]
	
	for a = 1,49 do
      if string.match(E[a], '.*(2012).*') == '2012' then
         E[a] = string.gsub(E[a], '.*(Jan) ', '000 + ', 1)
         E[a] = string.gsub(E[a], '.*(Feb) ', '031 + ', 1)
         E[a] = string.gsub(E[a], '.*(Mar) ', '060 + ', 1)
         E[a] = string.gsub(E[a], '.*(Apr) ', '091 + ', 1)
         E[a] = string.gsub(E[a], '.*(May) ', '121 + ', 1)
         E[a] = string.gsub(E[a], '.*(Jun) ', '152 + ', 1)
         E[a] = string.gsub(E[a], '.*(Jul) ', '182 + ', 1)
         E[a] = string.gsub(E[a], '.*(Aug) ', '213 + ', 1)
         E[a] = string.gsub(E[a], '.*(Sep) ', '244 + ', 1)
         E[a] = string.gsub(E[a], '.*(Oct) ', '274 + ', 1)
         E[a] = string.gsub(E[a], '.*(Nov) ', '305 + ', 1)
         E[a] = string.gsub(E[a], '.*(Dec) ', '335 + ', 1)
      elseif string.match(E[a], '.*(2016).*') == '2016' then
         E[a] = string.gsub(E[a], '.*(Jan) ', '000 + ', 1)
         E[a] = string.gsub(E[a], '.*(Feb) ', '031 + ', 1)
         E[a] = string.gsub(E[a], '.*(Mar) ', '060 + ', 1)
         E[a] = string.gsub(E[a], '.*(Apr) ', '091 + ', 1)
         E[a] = string.gsub(E[a], '.*(May) ', '121 + ', 1)
         E[a] = string.gsub(E[a], '.*(Jun) ', '152 + ', 1)
         E[a] = string.gsub(E[a], '.*(Jul) ', '182 + ', 1)
         E[a] = string.gsub(E[a], '.*(Aug) ', '213 + ', 1)
         E[a] = string.gsub(E[a], '.*(Sep) ', '244 + ', 1)
         E[a] = string.gsub(E[a], '.*(Oct) ', '274 + ', 1)
         E[a] = string.gsub(E[a], '.*(Nov) ', '305 + ', 1)
         E[a] = string.gsub(E[a], '.*(Dec) ', '335 + ', 1)
      else
         E[a] = string.gsub(E[a], '.*(Jan) ', '000 + ', 1)
         E[a] = string.gsub(E[a], '.*(Feb) ', '031 + ', 1)
         E[a] = string.gsub(E[a], '.*(Mar) ', '059 + ', 1)
         E[a] = string.gsub(E[a], '.*(Apr) ', '090 + ', 1)
         E[a] = string.gsub(E[a], '.*(May) ', '120 + ', 1)
         E[a] = string.gsub(E[a], '.*(Jun) ', '151 + ', 1)
         E[a] = string.gsub(E[a], '.*(Jul) ', '181 + ', 1)
         E[a] = string.gsub(E[a], '.*(Aug) ', '212 + ', 1)
         E[a] = string.gsub(E[a], '.*(Sep) ', '243 + ', 1)
         E[a] = string.gsub(E[a], '.*(Oct) ', '273 + ', 1)
         E[a] = string.gsub(E[a], '.*(Nov) ', '304 + ', 1)
         E[a] = string.gsub(E[a], '.*(Dec) ', '334 + ', 1)
      end
      
      E[a] = string.gsub(E[a], ', (2011).*', ' + 0000', 1)
      E[a] = string.gsub(E[a], ', (2012).*', ' + 0365', 1)
      E[a] = string.gsub(E[a], ', (2013).*', ' + 0731', 1)
      E[a] = string.gsub(E[a], ', (2014).*', ' + 1096', 1)
      E[a] = string.gsub(E[a], ', (2015).*', ' + 1462', 1)
      E[a] = string.gsub(E[a], ', (2016).*', ' + 1872', 1)
      
      E[a] = tonumber(E[a])
   end
   
   --[[
   Same formula from above, applied to the current date so that the event date can be compared to the current date.
   --]]
   
   curdate = os.date("%m %d %Y")
   if string.match(curdate, '2012') == '2012' then
      curdate = string.gsub(curdate, '(01) ', '000 + ', 1)
      curdate = string.gsub(curdate, '(02) ', '031 + ', 1)
      curdate = string.gsub(curdate, '(03) ', '060 + ', 1)
      curdate = string.gsub(curdate, '(04) ', '091 + ', 1)
      curdate = string.gsub(curdate, '(05) ', '121 + ', 1)
      curdate = string.gsub(curdate, '(06) ', '152 + ', 1)
      curdate = string.gsub(curdate, '(07) ', '182 + ', 1)
      curdate = string.gsub(curdate, '(08) ', '213 + ', 1)
      curdate = string.gsub(curdate, '(09) ', '244 + ', 1)
      curdate = string.gsub(curdate, '(10) ', '274 + ', 1)
      curdate = string.gsub(curdate, '(11) ', '305 + ', 1)
      curdate = string.gsub(curdate, '(12) ', '335 + ', 1)
   elseif string.match(curdate, '2016') == '2016' then
      curdate = string.gsub(curdate, '(01) ', '000 + ', 1)
      curdate = string.gsub(curdate, '(02) ', '031 + ', 1)
      curdate = string.gsub(curdate, '(03) ', '060 + ', 1)
      curdate = string.gsub(curdate, '(04) ', '091 + ', 1)
      curdate = string.gsub(curdate, '(05) ', '121 + ', 1)
      curdate = string.gsub(curdate, '(06) ', '152 + ', 1)
      curdate = string.gsub(curdate, '(07) ', '182 + ', 1)
      curdate = string.gsub(curdate, '(08) ', '213 + ', 1)
      curdate = string.gsub(curdate, '(09) ', '244 + ', 1)
      curdate = string.gsub(curdate, '(10) ', '274 + ', 1)
      curdate = string.gsub(curdate, '(11) ', '305 + ', 1)
      curdate = string.gsub(curdate, '(12) ', '335 + ', 1)
   else
      curdate = string.gsub(curdate, '(01) ', '000 + ', 1)
      curdate = string.gsub(curdate, '(02) ', '031 + ', 1)
      curdate = string.gsub(curdate, '(03) ', '059 + ', 1)
      curdate = string.gsub(curdate, '(04) ', '090 + ', 1)
      curdate = string.gsub(curdate, '(05) ', '120 + ', 1)
      curdate = string.gsub(curdate, '(06) ', '151 + ', 1)
      curdate = string.gsub(curdate, '(07) ', '181 + ', 1)
      curdate = string.gsub(curdate, '(08) ', '212 + ', 1)
      curdate = string.gsub(curdate, '(09) ', '243 + ', 1)
      curdate = string.gsub(curdate, '(10) ', '273 + ', 1)
      curdate = string.gsub(curdate, '(11) ', '304 + ', 1)
      curdate = string.gsub(curdate, '(12) ', '334 + ', 1)
   end
   
   curdate = string.gsub(curdate, ' (2011)', ' + 0000', 1)
   curdate = string.gsub(curdate, ' (2012)', ' + 0365', 1)
   curdate = string.gsub(curdate, ' (2013)', ' + 0731', 1)
   curdate = string.gsub(curdate, ' (2014)', ' + 1096', 1)
   curdate = string.gsub(curdate, ' (2015)', ' + 1462', 1)
   curdate = string.gsub(curdate, ' (2016)', ' + 1872', 1)
   
   curdate = tonumber(curdate)
   
   --[[
   Here's the comparison! Takes each event date, figures out how close to the current date it is, and then sorts it appropriately:
   if the event is tomorrow (one day away), the strings related to the event are sorted into the corresponding tables (d1, in this case).
   If the event is seven days or more away (anytime past a week), the strings are hidden because there is no need for them.
   --]]
   for c = 1, 49 do
      if E[c] - curdate < 7 then
         if E[c] - curdate == 0 then
            table.insert(d0, TL[c])
            table.insert(d0, TR[c])
         elseif E[c] - curdate == 1 then
            table.insert(d1, TL[c])
            table.insert(d1, TR[c])
         elseif E[c] - curdate == 2 then
            table.insert(d2, TL[c])
            table.insert(d2, TR[c])
         elseif E[c] - curdate == 3 then
            table.insert(d3, TL[c])
            table.insert(d3, TR[c])
         elseif E[c] - curdate == 4 then
            table.insert(d4, TL[c])
            table.insert(d4, TR[c])
         elseif E[c] - curdate == 5 then
            table.insert(d5, TL[c])
            table.insert(d5, TR[c])
         else
            table.insert(d6, TL[c])
            table.insert(d6, TR[c])
         end
      else
         TL[c]:Hide()
         TR[c]:Hide()
      end
   end
   
   return E[1]
   
   --[[
   There will be more to this, but until I can get this section working correctly to hide strings that are seven days away,
   I don't really want to put more work into it.
   --]]
   
end -- function Update
(Caveat: I'm almost as new to Lua as you are, so I'm still figuring this out as I go.)
User avatar
donutmtoazt
Posts: 50
Joined: July 10th, 2011, 9:15 am
Location: Fresno, CA

Re: Lua 'loadstring' problems

Post by donutmtoazt »

Wow, that seems a lot cleaner than my code. Ya, I would have used the For statement, but I just feel so uncomfortable with it for some reason. Maybe that means I should use it more?

Anyways, let me implement the ideas you had into the code and see if that clears everything up.

Thanks for all the ideas. I can't tell you how long this has bothered me.
This is my keyboard. There are many like it, but this one is mine.
User avatar
donutmtoazt
Posts: 50
Joined: July 10th, 2011, 9:15 am
Location: Fresno, CA

Re: Lua 'loadstring' problems

Post by donutmtoazt »

Hey Kaelri, I know this has been bothering you all day, so I figured I would update you on the situation. ;-)

So, I tried your code out and unfortunately, I did not receive the desired results.
Now, your cleanup in the Initialize function worked perfectly, and that was needed, but after trying the code, it looks as though 'tonumber' didn't work as planned. After working around with it, it appears that the operations going on in there make the 'tonumber' function return 'nil'. Who'd have guessed?

So, it looks like my only choice is to use the 'loadstring' function and play around with it until it returns something useful. Your help has returned some of my hope in this code, so I'll brainstorm on this for a day or two before coming back for some more help.

Thanks for restoring my motivation in this!
This is my keyboard. There are many like it, but this one is mine.
poiru
Developer
Posts: 2872
Joined: April 17th, 2009, 12:18 pm

Re: Lua 'loadstring' problems

Post by poiru »

You could solve your issue with getfenv/setfenv:

Code: Select all

	E = {}
	E[0] = "212 + 22 + 0000"
	env = getfenv(1)
	f = loadstring("E[0] = " .. E[0])
	setfenv(f, env)
	f()
	return E[0]
However, I would avoid getfenv/setfenv as I belive they are going to be removed in Lua 5.2.

Something like the following would be a better long-term solution:

Code: Select all

	E = {}
	E[0] = "212 + 22 + 0000"
	f = loadstring("return " .. E[0])
	E[0] = f()
	return E[0]
User avatar
Yggdrasil
Posts: 24
Joined: June 25th, 2011, 5:09 pm

Re: Lua 'loadstring' problems

Post by Yggdrasil »

Every global value is in a global table called _G. So, you can use this:

Code: Select all

for i=1,49 do
    _G["E"..i] = SKIN:GetMeasure("mTime"..i)
    _G["TL"..i] = SKIN:GetMeter("Item"..i.."Left")
    _G["TR"..i] = SKIN:GetMeter("Item"..i.."Right")
end
This way you can access them as simple global values like E1, TL1, TR1 ... E49, TL49, TR49. But the table way is better, because it's faster, if you declare it local:

Code: Select all

local E,TL,TR = {},{},{} -- three local table to store the values
for i = 1, 49 do
      local msMeasure = SKIN:GetMeasure("mTime"..i)
      E[i] = msMeasure:GetStringValue()
      TL[i] = SKIN:GetMeter("Item"..i.."Left")
      TR[i] = SKIN:GetMeter("Item"..i.."Right")
end
When you use Lua, always try to use local variables, where possible, because they are always faster. By the way, poiru is right, setfenv is deprecated in Lua5.2.
Image
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Lua 'loadstring' problems

Post by jsmorley »

Just a tip for those new to this:

Be sure not to use "local" when collecting a bunch of meters or measures into a table in Initialize() since you only need to do it once, then try to reference that table in Update(). That won't work. Leave the table as default "global" in that case.
User avatar
donutmtoazt
Posts: 50
Joined: July 10th, 2011, 9:15 am
Location: Fresno, CA

Re: Lua 'loadstring' problems

Post by donutmtoazt »

Sweet!! poiru's edit to my 'loadstring' function worked, and now the code is running smoothly.
I also had an issue with my TL and TR tables until I realized I used GetMeasure instead of GetMeter. But let's not talk about that... :oops:

Anyways, thanks to everyone that replied to this topic. You guys are always helpful!
This is my keyboard. There are many like it, but this one is mine.