It is currently September 21st, 2019, 5:30 am

Calling a single line from a txt file in lua

Discuss the use of Lua in Script measures.
Mayal0
Posts: 3
Joined: March 17th, 2014, 4:32 pm

Calling a single line from a txt file in lua

Mayal0 » March 17th, 2014, 6:44 pm

So I'm trying to write a lua script to call a specific line from two separate txt files of the same length. It's a sort of joke of the day type script that needs a riddle and an answer. My problem so far is either with retrieving the data from the file or calling up the specific line. I'm very new to the script and was able to get this far in about 1 day, and have been trying for the past day or 2 just to figure out where it is failing. Could someone please take a look at this code and tell me if I'm doing something wrong that is simple or recommend a way for me to go about this differently?

riddleio.lua

Code: Select all

function Initialize()
	
	mtRiddle = SKIN:GetMeter("RiddleMeter")
	mtAnswer = SKIN:GetMeter("AnswerMeter")
	
end

function Update()
	
	RiddleFilePath = "C:\Users\Alex\Documents\Rainmeter\Skins\mayal0\@Resources\riddles.txt"
	AnswerFilePath = "C:\Users\Alex\Documents\Rainmeter\Skins\mayal0\@Resources\answers.txt"
	luarand = math.random(1,71)
	ostime = os.date('*t')
	modday = math.fmod(ostime.yday,71)+1
	
	RiddleTodayTable = ReadFileLines(RiddleFilePath)
	RiddleToday = RiddleTodayTable[modday]
	AnswerTodayTable = ReadFileLines(AnswerFilePath)
	AnswerToday = AnswerTodayTable[modday]
	
	SKIN:Bang('!SetOption RiddleMeter Text '..RiddleToday)
	SKIN:Bang('!SetOption AnswerMeter Text '..AnswerToday)
	
end

ReadFileLines = function(FilePath)
	FilePath = SKIN:MakePathAbsolute(FilePath)

	local File = io.open(FilePath)

	if not File then
		print('ReadFile: unable to open file at ' .. FilePath)
		return
	end

	local Contents = {}
	for Line in File:lines() do
		table.insert(Contents, Line)
	end

	File:close()

	return Contents
end
User avatar
smurfier
Moderator
Posts: 1937
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

Re: Calling a single line from a txt file in lua

smurfier » March 17th, 2014, 10:34 pm

The forward slash (\) is the escape character in Lua. This means that when you use a forward slash you have to escape it.

"C:\\Users\\Alex\\Documents\\Rainmeter\\Skins\\mayal0\\@Resources\\riddles.txt"

To avoid this I usually just use back slashes when specifying file paths in Lua since Windows will allow the use of either.
GitHub | DeviantArt | Tumblr
This is the song that never ends. It just goes on and on my friends. Some people started singing it not knowing what it was, and they'll continue singing it forever just because . . .
Mayal0
Posts: 3
Joined: March 17th, 2014, 4:32 pm

Re: Calling a single line from a txt file in lua

Mayal0 » March 18th, 2014, 4:05 pm

So I've tried switching it the slashes and it didn't fix the problem. I have it set up right now to display the value of modday for the RiddleMeter and then display the what should be the correct answer string in the AnswerMeter. It's showing the test string, but not the answer. Could it be something wrong with my readfilelines function or me not calling the position of the outputted table properly? Could it be trying to find a position in the table labeled "modday" instead of the value of the variable modday?

Code: Select all

function Initialize()
	
	mtRiddle = SKIN:GetMeter("RiddleMeter")
	mtAnswer = SKIN:GetMeter("AnswerMeter")
	
end

function Update()
	
	RiddleFilePath = "C:/Users/Alex/Documents/Rainmeter/Skins/mayal0/@Resources/riddles.txt"
	AnswerFilePath = "C:/Users/Alex/Documents/Rainmeter/Skins/mayal0/@Resources/answers.txt"
	luarand = math.random(1,71)
	ostime = os.date('*t')
	modday = math.fmod(ostime.yday,71)+1
	
	RiddleTodayTable = ReadFileLines(RiddleFilePath)
	RiddleToday = RiddleTodayTable[modday]
	AnswerTodayTable = ReadFileLines(AnswerFilePath)
	AnswerToday = AnswerTodayTable[modday]
	
	SKIN:Bang('!SetOption RiddleMeter Text '..modday)
	SKIN:Bang('!SetOption AnswerMeter Text '..RiddleToday)
	
end

ReadFileLines = function(FilePath)
	FilePath = SKIN:MakePathAbsolute(FilePath)

	local File = io.open(FilePath)

	if not File then
		print('ReadFile: unable to open file at ' .. FilePath)
		return
	end

	local Contents = {}
	for Line in File:lines() do
		table.insert(Contents, Line)
	end

	File:close()

	return Contents
end
User avatar
smurfier
Moderator
Posts: 1937
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

Re: Calling a single line from a txt file in lua

smurfier » March 18th, 2014, 4:18 pm

Try running this script. If it doesn't work, check the About dialog for errors.

Code: Select all

function Initialize()
	RiddleTodayTable = ReadFileLines(SKIN:ReplaceVariables('#@#riddles.txt'))
	AnswerTodayTable = ReadFileLines(SKIN:ReplaceVariables('#@#answers.txt'))
end

function Update()
	local ostime = os.date('*t')
	local modday = math.fmod(ostime.yday, 71)+1

	SKIN:Bang('!SetOption', 'RiddleMeter', 'Text', RiddleTodayTable[modday])
	SKIN:Bang('!SetOption', 'AnswerMeter', 'Text', AnswerTodayTable[modday])
end

ReadFileLines = function(FilePath)
	FilePath = SKIN:MakePathAbsolute(FilePath)

	local File = io.open(FilePath, 'r')
	local Contents = {}

	if File then
		for Line in File:lines() do
			table.insert(Contents, Line)
		end
		File:close()
	else
		print('ReadFile: unable to open file at ' .. FilePath)
	end

	return Contents
end
GitHub | DeviantArt | Tumblr
This is the song that never ends. It just goes on and on my friends. Some people started singing it not knowing what it was, and they'll continue singing it forever just because . . .
Mayal0
Posts: 3
Joined: March 17th, 2014, 4:32 pm

Re: Calling a single line from a txt file in lua

Mayal0 » March 18th, 2014, 4:38 pm

That worked. Thank you very much. Is there a reason that the tables were made in the initialize function section as opposed to the update function? Also, what purpose does local serve? Am I supposed to put that before any variables that I use that aren't used elsewhere in the script?

I want to make sure I learn from my mistakes. Thank you very much for your help, I really appreciate it.
User avatar
smurfier
Moderator
Posts: 1937
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

Re: Calling a single line from a txt file in lua

smurfier » March 18th, 2014, 5:09 pm

Mayal0 wrote:That worked. Thank you very much. Is there a reason that the tables were made in the initialize function section as opposed to the update function? Also, what purpose does local serve? Am I supposed to put that before any variables that I use that aren't used elsewhere in the script?

I want to make sure I learn from my mistakes. Thank you very much for your help, I really appreciate it.
Reading the files only once instead of on each update is a good idea. Means a lot less work is being done.

You should use local variables when you can. They are "faster" than global variables for whatever reason. Yes, it is for variables that you don't use anywhere else. They only exist in the scope in which you use them.
GitHub | DeviantArt | Tumblr
This is the song that never ends. It just goes on and on my friends. Some people started singing it not knowing what it was, and they'll continue singing it forever just because . . .