0

I want string to use variable.

variable1 = "Hello"
a = "variable" .. 1
print(a)

but it printed variable1 (of cource),
Can I print "Hello"(variable1) using variable a?

2
  • 1
    You can use table for this. Like @cubic said. Commented Jul 10, 2022 at 18:03
  • print('"Hello"(' .. a .. ')') if that is not what you want you should improve your question. Commented Jul 11, 2022 at 4:52

1 Answer 1

3

Yes, this is exactly what tables are for - you can just rewrite your code as

local variables = {}
variables.variable1 = "Hello"
local a = "variable" .. 1
print(variables[a]) -- prints Hello

In fact, global variables (as in your example you seem to be using) are already stored in a table called _G (the global table), so in your example this would work:

variable1 = "Hello"
a = "variable" .. 1
print(_G[a])
Sign up to request clarification or add additional context in comments.

2 Comments

I don't see the relation between the question and your answer.
@Piglet Judging from your comment on the question, I'd chalk that up to you misreading the question.

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.