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.
It is currently March 29th, 2024, 8:36 am
Learning Lua.
-
- Developer
- Posts: 22628
- Joined: April 19th, 2009, 11:02 pm
- Location: Fort Hunt, Virginia, USA
-
- Rainmeter Sage
- Posts: 16110
- Joined: October 11th, 2010, 6:27 pm
- Location: Gheorgheni, Romania
Re: Learning Lua.
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.jsmorley wrote:But basically you are going to want to replace (w) or add to (a) the file.
-
- Posts: 919
- Joined: January 30th, 2017, 2:01 am
- Location: Greece
Re: Learning Lua.
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+"?
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.
-
- Developer
- Posts: 22628
- Joined: April 19th, 2009, 11:02 pm
- Location: Fort Hunt, Virginia, USA
Re: Learning Lua.
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.
And no. "a+" is to append to the end of the file.
-
- Posts: 919
- Joined: January 30th, 2017, 2:01 am
- Location: Greece
Re: Learning Lua.
Ok, because I don't want to bother you anymore, could you write an example that changes the content of a specific line?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.
Test.txt file
Code: Select all
a
a
a
a
a
ccc
a
a
How would I replace it with lua?
-
- Developer
- Posts: 22628
- Joined: April 19th, 2009, 11:02 pm
- Location: Fort Hunt, Virginia, USA
Re: Learning Lua.
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
-
- Posts: 919
- Joined: January 30th, 2017, 2:01 am
- Location: Greece
Re: Learning Lua.
Okay I love you.
I'll try to do the rest on my own
I'll try to do the rest on my own
-
- Posts: 919
- Joined: January 30th, 2017, 2:01 am
- Location: Greece
Re: Learning Lua.
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?
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.
-
- Posts: 919
- Joined: January 30th, 2017, 2:01 am
- Location: Greece
Re: Learning Lua.
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?
-
- Developer
- Posts: 22628
- Joined: April 19th, 2009, 11:02 pm
- Location: Fort Hunt, Virginia, USA
Re: Learning Lua.
The characters ^ $ ( ) % . [ ] * + - ? are reserved when used in any pattern matching functions: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?
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