Is there a way to assign a value to a variable, but inside a table?
Yes there is
Do you mean⌠inside a table??? or under it?
Really under a table. Sweeping the elements and assigning a value. As is usually done on other occasions.
Something like this?
local t = {}
t["first"] = 1
Mmm, something like that. But that doesnât work. The value it takes is null. It is never one.
bgmadclown example is correct, to get the value stored in âfirstâ and print it, you should do print(t["first"]
or print(t.first)
, both work the same.
For more information about how Lua tables work (Tables are basically everything in this language) check this link..
Or maybe you want the table to work as a variable?
local t = {}
t[âfirstâ] = 1
print(t.first)
if first==1 then
print (âBienâ)
else
print(âMalâ)
end
Mmm⌠Something is wrong.
Yes, instead, it should be:
if t.first == 1 then
print (âAhora siâ)
else
print(â:(â)
end
The only difference is t.first
instead of just first
To add to @depilz comment,
first
refers to a variable called âfirstâ, whereas t.first
refers to an entry (or a key) called âfirstâ that is inside the table t
.
These two are entirely different and should not be confused.
Exactly. I need it to be the same variable before and after going through the table. This would save me a lot of lines of code.
What you are describing is not possible - variables do not âpass throughâ tables. As XeduR has described, first and t.first do not refer to the same thing - the fact that they have similar names is irrelevant.
Why do you need it to be a separate variable and also part of a table?
Can you not just get rid of the variable and make sure it is always part of the table instead (e.g. replace all instances of first with t.first throughout your code)?