It is currently September 20th, 2019, 5:31 pm

Separate data and code?

Discuss the use of Lua in Script measures.
teacherfeature
Posts: 3
Joined: December 20th, 2017, 6:29 am

Separate data and code?

teacherfeature » December 20th, 2017, 7:05 am

My Lua script has 80 lines of code and about 700 lines of data. I would like to split code and data into two files. Lua offers the require https://www.lua.org/pil/8.1.html function as a preferred way to do this. Rainmeter does not support require https://docs.rainmeter.net/manual/lua-scripting/ (see under restrictions). What is the usual way to solve this problem? Is it possible at all? BTW: dofile does not seem to be a viable solution. Thank you for every contribution.
User avatar
jsmorley
Developer
Posts: 19467
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Separate data and code?

jsmorley » December 20th, 2017, 2:39 pm

Not sure I follow. What do you mean by "data"?

Are you looking to populate a table with string or numeric data you want to read from an external file? If so, I see no need for a dofile() or any other trick at all. Just open and read the file and populate the table in the Iniitialize() function of your Lua. I can't picture any other definition of "data" in this this context. I mean at the end of the day it's all variable assignments isn't it. Whether it be myVar=1 or myTable[index] =1.
teacherfeature
Posts: 3
Joined: December 20th, 2017, 6:29 am

Re: Separate data and code?

teacherfeature » December 20th, 2017, 6:07 pm

Thanks. I was trapped in the view of treating the data as code. But you're absolutely right. In the end, it's all about filling a table with values. So simple.
User avatar
jsmorley
Developer
Posts: 19467
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Separate data and code?

jsmorley » December 20th, 2017, 6:23 pm

teacherfeature wrote:Thanks. I was trapped in the view of treating the data as code. But you're absolutely right. In the end, it's all about filling a table with values. So simple.
Glad to help... Here is an example:
RandomQuote_1.0.rmskin
1.png

Code: Select all

function Initialize()
   
	quoteTable, currentTable = {}, {}
   
	for line in io.lines(SKIN:MakePathAbsolute('Pithy.txt')) do
		table.insert(quoteTable, line)
	end
	
	SKIN:Bang('!Log', #quoteTable..' items loaded', 'Debug')
   
end

function Update()

	if #currentTable == 0 then
		currentTable = SortRandom(quoteTable)
	end
	
	return table.remove(currentTable)
   
end

function SortRandom(inputTable)

	assert(type(inputTable) == 'table', ('SortRandom must receive a table. Received %s instead.'):format(type(inputTable)))

	local sortedTable, unsortedTable = {}, {}
	
	for inc = 1, #inputTable do
		table.insert(unsortedTable, inputTable[inc])
	end

	math.randomseed(tonumber(tostring(os.time()):reverse():sub(1,6)))

	while #unsortedTable > 0 do
		table.insert(sortedTable, table.remove(unsortedTable, math.random(1, #unsortedTable)))
	end
	
	unsortedTable = nil

	return sortedTable
	
end
You do not have the required permissions to view the files attached to this post.
teacherfeature
Posts: 3
Joined: December 20th, 2017, 6:29 am

Re: Separate data and code?

teacherfeature » December 21st, 2017, 8:36 am

The example is very helpful (and the sayings are funny). Thank you again.