It is currently March 28th, 2024, 11:56 am

Separate data and code?

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

Separate data and code?

Post by teacherfeature »

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: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Separate data and code?

Post by jsmorley »

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?

Post by teacherfeature »

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: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Separate data and code?

Post by jsmorley »

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
(9.25 KiB) Downloaded 64 times
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
teacherfeature
Posts: 3
Joined: December 20th, 2017, 6:29 am

Re: Separate data and code?

Post by teacherfeature »

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