Page 1 of 1
Ask about the syntax of lua
Posted: Wed Mar 09, 2022 12:26 pm
by sdgmlj
The results I want to achieve are as follows:
s1="setting1"
s2="setting2"
s3="setting3"
...
s100="setting100"
That's what I wrote:
for i=1,100 do
.......="setting"..tostring(i) end
How do you write in front of the equal sign?
Re: Ask about the syntax of lua
Posted: Wed Mar 09, 2022 12:36 pm
by Stringweasel
I would do something like this
Code: Select all
local prefix = "setting"
local settings = {}
for i=1,5 do
settings[i] = prefix..i
print(settings[i])
end
Which will print:
Code: Select all
setting1
setting2
setting3
setting4
setting5
Re: Ask about the syntax of lua
Posted: Wed Mar 09, 2022 3:10 pm
by sdgmlj
Stringweasel wrote: ↑Wed Mar 09, 2022 12:36 pm
I would do something like this
Code: Select all
local prefix = "setting"
local settings = {}
for i=1,5 do
settings[i] = prefix..i
print(settings[i])
end
Which will print:
Code: Select all
setting1
setting2
setting3
setting4
setting5
Thank you, my friend. I think I understand. Thank you very much
Re: Ask about the syntax of lua
Posted: Wed Mar 09, 2022 3:37 pm
by quyxkh
If you *really* want the suffixes pasted on the end like that, global variables are in a table named `_G`.
Code: Select all
for x = 1,100 do _G['setting'..x]='setting'..x end
Re: Ask about the syntax of lua
Posted: Wed Mar 09, 2022 4:05 pm
by sdgmlj
quyxkh wrote: ↑Wed Mar 09, 2022 3:37 pm
If you *really* want the suffixes pasted on the end like that, global variables are in a table named `_G`.
Code: Select all
for x = 1,100 do _G['setting'..x]='setting'..x end
Is that what it says?
for x = 1,100 do _ G['s'..x]='setting'.. x end
Do I need quotation marks when I use this variable in the future?
*****=S1 or ***** = "S1"?
Re: Ask about the syntax of lua
Posted: Wed Mar 09, 2022 4:12 pm
by quyxkh
No, global variable names in your code are looked up in that table, and one of the global variable names is `_G`, whose value is self-referentially that table. So after `_G['howd'..'y']='hoo'..'hah!'` you can `print(howdy)`.
By the way, this is your opportunity to produce really horrible code. Do it the way Stringweasel suggests unless you've got articulable, concrete reasons to use the underlying machinery directly.
Re: Ask about the syntax of lua
Posted: Wed Mar 09, 2022 4:24 pm
by sdgmlj
quyxkh wrote: ↑Wed Mar 09, 2022 4:12 pm
No, global variable names in your code are looked up in that table, and one of the global variable names is `_G`, whose value is self-referentially that table. So after `_G['howd'..'y']='hoo'..'hah!'` you can `print(howdy)`.
By the way, this is your opportunity to produce really horrible code. Do it the way Stringweasel suggests unless you've got articulable, concrete reasons to use the underlying machinery directly.
Thank you. I see. I've learned something new again. I'll use it carefully.