Ask about the syntax of lua

Place to get help with not working mods / modding interface.
Post Reply
sdgmlj
Fast Inserter
Fast Inserter
Posts: 127
Joined: Sun Jul 04, 2021 11:12 pm
Contact:

Ask about the syntax of lua

Post 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?

User avatar
Stringweasel
Filter Inserter
Filter Inserter
Posts: 310
Joined: Thu Apr 27, 2017 8:22 pm
Contact:

Re: Ask about the syntax of lua

Post 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
Alt-F4 Author | Factorio Modder
Mods: Hall of Fame | Better Victory Screen | Fluidic Power | Biter Power | Space Spidertron | Spidertron Dock | Weasel's Demolition Derby

sdgmlj
Fast Inserter
Fast Inserter
Posts: 127
Joined: Sun Jul 04, 2021 11:12 pm
Contact:

Re: Ask about the syntax of lua

Post 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

quyxkh
Smart Inserter
Smart Inserter
Posts: 1027
Joined: Sun May 08, 2016 9:01 am
Contact:

Re: Ask about the syntax of lua

Post 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

sdgmlj
Fast Inserter
Fast Inserter
Posts: 127
Joined: Sun Jul 04, 2021 11:12 pm
Contact:

Re: Ask about the syntax of lua

Post 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"?

quyxkh
Smart Inserter
Smart Inserter
Posts: 1027
Joined: Sun May 08, 2016 9:01 am
Contact:

Re: Ask about the syntax of lua

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

sdgmlj
Fast Inserter
Fast Inserter
Posts: 127
Joined: Sun Jul 04, 2021 11:12 pm
Contact:

Re: Ask about the syntax of lua

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

Post Reply

Return to “Modding help”