2

I want to use a for loop to create multiple varibles (with names that are the same except for the last character) in lua

for i= 1, 10, 1 do
        marker+i = "do things"
    end

pretty much I what I want to get is: marker0, marker1, marker2 and so on. and I guess there is something wrong with marker+i

I get an error. Thank you.

1
  • I think you'd be much better off creating a table: marker={}, marker[i]=.... Commented Nov 25, 2014 at 17:43

1 Answer 1

3

You probably don't want to do this actually. Much simpler would be to create a table and create those variables as keys in the table.

t={}
for i=1, 10, 1 do
    t["marker"..i] = "do things"
end

(Note that .. is contatenation and not + in lua. Note also that you need to quote a string and not use it literally.)

But if you really want those to be global variables and not keys in some other table you can generally (depending on environment) do the following

for i=1, 10, 1 do
    _G["marker"..i] = "do things"
end
Sign up to request clarification or add additional context in comments.

2 Comments

Or simply use marker as the table name and index it directly: marker[i].
@lhf Indeed. I assumed the constructed variable names had a point in themsevles but that may not be the case either.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.