Page 3 of 4

Re: Learning Lua.

Posted: September 27th, 2017, 6:11 pm
by jsmorley
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.

Re: Learning Lua.

Posted: September 27th, 2017, 6:16 pm
by balala
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.

Re: Learning Lua.

Posted: September 27th, 2017, 6:23 pm
by kyriakos876
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+"?

Re: Learning Lua.

Posted: September 27th, 2017, 6:24 pm
by jsmorley
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.

Re: Learning Lua.

Posted: September 27th, 2017, 6:27 pm
by kyriakos876
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?

Re: Learning Lua.

Posted: September 27th, 2017, 6:39 pm
by jsmorley

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

Re: Learning Lua.

Posted: September 27th, 2017, 6:45 pm
by kyriakos876
Okay I love you.

I'll try to do the rest on my own :)

Re: Learning Lua.

Posted: September 27th, 2017, 11:48 pm
by kyriakos876
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?

Re: Learning Lua.

Posted: September 28th, 2017, 12:18 pm
by kyriakos876
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?

Re: Learning Lua.

Posted: September 28th, 2017, 12:24 pm
by jsmorley
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