Assign values to variables inside a table

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.

1 Like

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.

1 Like

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