It is currently April 19th, 2024, 8:57 pm

the last line to the first line, when read a txt file

Discuss the use of Lua in Script measures.
comic3344
Posts: 13
Joined: November 13th, 2012, 4:17 am

the last line to the first line, when read a txt file

Post by comic3344 »

code:

Code: Select all

function Initialize()
       file = io.open(SKIN:MakePathAbsolute('words.txt'), 'r')
end -- function Initialize

function Update()
                 
         wordsline = file:read("*line")
         SKIN:Bang('!SetOption MeterStrMW Text '..'\" '..wordsline..' \"')
         -- if wordline == nil then 
         --    file:close()
         --    SKIN:Bang('!Refresh') 
         -- end

end -- function Update
i use the "if" to change the position, but something wrong in it~

Please help me, thank you~ :-(
User avatar
MerlinTheRed
Rainmeter Sage
Posts: 889
Joined: September 6th, 2011, 6:34 am

Re: the last line to the first line, when read a txt file

Post by MerlinTheRed »

So you want the script to output a new line from the text wile with each update cycle? Looks OK to me, but you have misspelled wordsline in the part you commented out. I think you might even simplify your code by just using

Code: Select all

if not wordsline then 
-- ...
end
Have more fun creating skins with Sublime Text 2 and the Rainmeter Package!
User avatar
smurfier
Moderator
Posts: 1931
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

Re: the last line to the first line, when read a txt file

Post by smurfier »

You probably don't want open a file and leave it open at all times. You would not be able to delete it or move it at all.

The following code will read all the lines of a file into a table, closing the file when it's done. Then on each update it will output the next line, looping around to the beginning again when it's done.

Code: Select all

function Initialize()
	local file = io.open(SKIN:MakePathAbsolute('words.txt'), 'r')
	fLines = {}
	for line in file:lines() do
		table.insert(fLines, line)
	end
	file:close()
	cLine = 1
end -- function Initialize

function Update()
	SKIN:Bang('!SetOption', 'MeterStrMW', 'Text', fLines[cLine])
	cLine = cLine % #fLines + 1
end -- function Update
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 . . .
comic3344
Posts: 13
Joined: November 13th, 2012, 4:17 am

Re: the last line to the first line, when read a txt file

Post by comic3344 »

MerlinTheRed wrote:So you want the script to output a new line from the text wile with each update cycle? Looks OK to me, but you have misspelled wordsline in the part you commented out. I think you might even simplify your code by just using

Code: Select all

if not wordsline then 
-- ...
end
yes, it's my mistake~ so sorry~ HAHA~ :)

now it's ok~~ :) thank you ~
comic3344
Posts: 13
Joined: November 13th, 2012, 4:17 am

Re: the last line to the first line, when read a txt file

Post by comic3344 »

smurfier wrote:You probably don't want open a file and leave it open at all times. You would not be able to delete it or move it at all.

The following code will read all the lines of a file into a table, closing the file when it's done. Then on each update it will output the next line, looping around to the beginning again when it's done.

Code: Select all

function Initialize()
	local file = io.open(SKIN:MakePathAbsolute('words.txt'), 'r')
	fLines = {}
	for line in file:lines() do
		table.insert(fLines, line)
	end
	file:close()
	cLine = 1
end -- function Initialize

function Update()
	SKIN:Bang('!SetOption', 'MeterStrMW', 'Text', fLines[cLine])
	cLine = cLine % #fLines + 1
end -- function Update

Thank you! The code is what i want ~~ :D