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?
Ask about the syntax of lua
- Stringweasel
- Filter Inserter
- Posts: 310
- Joined: Thu Apr 27, 2017 8:22 pm
- Contact:
Re: Ask about the syntax of lua
I would do something like this
Which will print:
Code: Select all
local prefix = "setting"
local settings = {}
for i=1,5 do
settings[i] = prefix..i
print(settings[i])
end
Code: Select all
setting1
setting2
setting3
setting4
setting5
Alt-F4 Author | Factorio Modder
Mods: Hall of Fame | Better Victory Screen | Fluidic Power | Biter Power | Space Spidertron | Spidertron Dock | Weasel's Demolition Derby
Mods: Hall of Fame | Better Victory Screen | Fluidic Power | Biter Power | Space Spidertron | Spidertron Dock | Weasel's Demolition Derby
Re: Ask about the syntax of lua
Thank you, my friend. I think I understand. Thank you very muchStringweasel wrote: ↑Wed Mar 09, 2022 12:36 pmI would do something like thisWhich will print:Code: Select all
local prefix = "setting" local settings = {} for i=1,5 do settings[i] = prefix..i print(settings[i]) end
Code: Select all
setting1 setting2 setting3 setting4 setting5
Re: Ask about the syntax of lua
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
Is that what it says?quyxkh wrote: ↑Wed Mar 09, 2022 3:37 pmIf 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
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
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.
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
Thank you. I see. I've learned something new again. I'll use it carefully.quyxkh wrote: ↑Wed Mar 09, 2022 4:12 pmNo, 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.