It is currently March 28th, 2024, 12:25 pm

Learning Lua.

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

Re: Learning Lua.

Post by jsmorley »

The [[ and ]] characters are mostly used in a string to help solve problems with embedded literal " (quote) and ' (single quote) characters. It's called a "long bracket" and can solve a problem like:

myString = 'This string has 'single quotes' in it'
That is an issue, as the embedded single quotes will confuse things.

You can:

myString='This string has \'single quotes\' in it'
But that can be cumbersome with a long string.

So instead you can:

myString=[[This string has 'single quotes' in it]]
The [[ and ]] are seen as a special kind of quote for the string. The sequence was chosen as you are somewhat unlikely to have [[ or ]] in a literal string, while needing quotes or single quotes is certainly possible.

You can deal with embedded [[ or ]], but that just gets more involved than I think is useful here. You won't use this much if at all.
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Learning Lua.

Post by kyriakos876 »

jsmorley wrote: ...

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 \\.

...
Funny thing is, I tried escaping both brackets with / but it would still erase everything in the skin. One thing I also don't understand is, lua, is a case sensitive language so why in the line:
i, j = string.find(string.lower(value), string.format('meterbox%s]' , NumToDelete))

if I change meterbox to MeterBox which is the actual text in the .ini file it won't match it. What is going on here?
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Learning Lua.

Post by jsmorley »

Funny thing is, I tried escaping both brackets with / but it would still erase everything in the skin.
Escape is \ not /
One thing I also don't understand is, lua, is a case sensitive language so why in the line:
i, j = string.find(string.lower(value), string.format('meterbox%s]' , NumToDelete))

if I change meterbox to MeterBox which is the actual text in the .ini file it won't match it. What is going on here?

The point of this is that you are changing the value it is "reading" to lower case. So when you "search", you need to use lower case. What we are trying to do is get it to "apples and apples", so you don't have to say "find meterbox or MeterBox or meterBox or Meterbox or METERBOX"

Don't let this throw you. If you know the exact case of what you are getting, by all means remove the string.lower() and search for stuff using the "real" case.

I just always use string.lower() in situations like this, so I just don't have to worry about case. You can have something you distribute to users that is looking for a particular case for some text, and the user accidentally or carelessly changes "Hello World" to "hello world" and it all falls apart. I'd just prefer to avoid that if I can.

BTW, when you say "erasing everything in the skin", that indicates to me that you might not be adding \n (linefeed) to lines you are writing to the output file.

io.write(fileHandle, 'Line 1')
io.write(fileHandle, 'Line 2')

That will end up with one line in the file, just "Line 2". The second write is just overwriting the first one.

io.write(fileHandle, 'Line 1\n')
io.write(fileHandle, 'Line 2\n')

That will give you two lines in the file.
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Learning Lua.

Post by kyriakos876 »

jsmorley wrote:Escape is \ not /
Yea My bad, I meant \. I
jsmorley wrote: The point of this is that you are changing the value it is "reading" to lower case. So when you "search", you need to use lower case. What we are trying to do is get it to "apples and apples", so you don't have to say "find meterbox or MeterBox or meterBox or Meterbox or METERBOX"

Don't let this throw you. If you know the exact case of what you are getting, by all means remove the string.lower() and search for stuff using the "real" case.

I just always use string.lower() in situations like this, so I just don't have to worry about case. You can have something you distribute to users that is looking for a particular case for some text, and the user accidentally or carelessly changes "Hello World" to "hello world" and it all falls apart. I'd just prefer to avoid that if I can.
Actually my bad again.

I tried doing the exact match because I didn't know what string.lower() I added it later for the reason you said above, so I don't have to care. But when I tried the exact string "MeterBox" without the string.lower() it wouldn't find it even if in the .ini file it was exactly like that "MeterBox" but when I changed to "meterbox" it found it as expected. (Again I had no problem with .txt files... This noly happened to .ini and .inc files that I tried.)
jsmorley wrote: BTW, when you say "erasing everything in the skin", that indicates to me that you might not be adding \n (linefeed) to lines you are writing to the output file.

That will give you two lines in the file.
No no, if you see my lua script (Line 42), I replace whatever I find with nil (pretty much erase the line), but if I replace the nil with "Changed text" I will get a .ini file full of "Changed text" which means that when it searched for "meterbox" it matched it with every single thing it parsed.

That's why I said it erases everything. Because it makes every line nil.
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Learning Lua.

Post by kyriakos876 »

See this video that I try escape the [ and ] and the skin below is exactly the code you saw on the video.
https://drive.google.com/open?id=0BxOyVix15ZNuLUYwUkNua0lfV2s

(Sorry I don't know how to capture my screen any better.)

Skin removed by me for safety purposes.
Post Reply