It is currently May 25th, 2019, 2:22 pm

Learning Lua.

Discuss the use of Lua in Script measures.
User avatar
jsmorley
Developer
Posts: 19245
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Learning Lua.

jsmorley » September 27th, 2017, 6:11 pm

1) Sure. I generally change tests like that to lower case, so I don't have to worry about the source having "Hello World" or "hello world" or any other combination of capitalization.

2) There are lots of ways to write to a file

https://www.tutorialspoint.com/lua/lua_file_io.htm

But basically you are going to want to replace (w) or add to (a) the file. If you want to change something and replace the contents, you will have to first read the entire file into a table, then change the row in the table (the line) you want, then write the entire table back out, replacing the existing file contents.
User avatar
balala
Rainmeter Sage
Posts: 8208
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Learning Lua.

balala » September 27th, 2017, 6:16 pm

jsmorley wrote:But basically you are going to want to replace (w) or add to (a) the file.
It's easy to understand this. w means write, and a means append. Or a r can be used for read, but this can't write anything to the file.
User avatar
kyriakos876
Posts: 914
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Learning Lua.

kyriakos876 » September 27th, 2017, 6:23 pm

Oh I missed the description of "w" that said "Write enabled mode that overwrites the existing file or creates a new file."

So if I want to edit a line I would need "a+"?
Last edited by kyriakos876 on September 27th, 2017, 6:24 pm, edited 1 time in total.
User avatar
jsmorley
Developer
Posts: 19245
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Learning Lua.

jsmorley » September 27th, 2017, 6:24 pm

There is a way to open a file with "r+" which is read/write mode, and then use file:seek to move to a specific byte location and write bytes of data, but I haven't really messed with it much. I just read in the entire file, change what I want, and read out the entire file.

And no. "a+" is to append to the end of the file.
User avatar
kyriakos876
Posts: 914
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Learning Lua.

kyriakos876 » September 27th, 2017, 6:27 pm

jsmorley wrote:There is a way to open a file with "r+" which is read/write mode, and then use file:seek to move to a specific byte location and write bytes of data, but I haven't really messed with it much. I just read in the entire file, change what I want, and read out the entire file.
Ok, because I don't want to bother you anymore, could you write an example that changes the content of a specific line?

Test.txt file

Code: Select all

a
a
a
a
a
ccc
a
a
I want for example to change ccc to aaa
How would I replace it with lua?
User avatar
jsmorley
Developer
Posts: 19245
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Learning Lua.

jsmorley » September 27th, 2017, 6:39 pm

Code: Select all

function Initialize()

   myTextFile = SKIN:GetVariable('CURRENTPATH')..'Test.txt'
   
end

function Update()

   fileContent = {}
   
   local file = io.open(myTextFile, 'r')
   local fileContent = {}
   for line in file:lines() do
      table.insert (fileContent, line)
   end
   
   io.close(file)
   
   for index, value in ipairs(fileContent) do
      i, j = string.find(string.lower(value), 'ccc')
      if j then
         fileContent[index] = 'aaa'
      end
   end
      
	 file = io.open(myTextFile, 'w')
	 for index, value in ipairs(fileContent) do
		file:write(value..'\n')
    end
	 
	 io.close(file)
		
end
User avatar
kyriakos876
Posts: 914
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Learning Lua.

kyriakos876 » September 27th, 2017, 6:45 pm

Okay I love you.

I'll try to do the rest on my own :)
User avatar
kyriakos876
Posts: 914
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Learning Lua.

kyriakos876 » September 27th, 2017, 11:48 pm

Soooo, I made some progress but I have a question.
In the "SLua.lua" file included in the skin below, line: 40, if I close the brackets, it will delete everything in the skin, which means that it sees everything as '[meterbox%s]'. I figured it has something to do with the syntax or the encoding of the file, as I do not have such problem with .txt files.

(Click the green box to add a meter and the red to remove the last one added. If you replace
the 'meterbox%s]' in the 40th line, of the .lua script, with '[meterbox%s]' and click the red button, the skin will become blank and close due to lack of meters.)

What am I missing?
Last edited by kyriakos876 on September 29th, 2017, 2:55 pm, edited 1 time in total.
User avatar
kyriakos876
Posts: 914
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Learning Lua.

kyriakos876 » September 28th, 2017, 12:18 pm

Is "[" a reserved character in lua for something specific ? Because I didn't find anything about single brackets in all of the links you provided me above. I only found that double brackets "[[ ]]" mean "string" but what about singles?
User avatar
jsmorley
Developer
Posts: 19245
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Learning Lua.

jsmorley » September 28th, 2017, 12:24 pm

kyriakos876 wrote:Is "[" a reserved character in lua for something specific ? Because I didn't find anything about single brackets in all of the links you provided me above. I only found that double brackets "[[ ]]" mean "string" but what about singles?
The characters ^ $ ( ) % . [ ] * + - ? are reserved when used in any pattern matching functions:

string.find
string.gmatch
string.gsub
string.match

This is similar to regular expression, except that they must be "escaped" using the % character.

https://www.lua.org/manual/5.3/manual.html#6.4.1
https://www.lua.org/pil/20.2.html
http://lua-users.org/wiki/PatternsTutorial

The characters ^ $ ( ) % . [ ] * + - ? are not seen as anything special in a string, only in the pattern component of any of the pattern matching functions above.

myString = 'This has a [SectionName] in it'
sectionName = string.match(myString, '.-%[(.-)%].-')

However, It gets a bit complicated, since in any string, not just pattern matching, the \ character is also an "escape", as it is how you do control characters like \n to get a linefeed. So to use a literal \ in a string, you would need \\.

myPath = 'C:\\MyFolder\\MySubFolder\\MyFile.txt'
fileHandle = io.open(myPath, 'r')

https://www.lua.org/pil/2.4.html