11 posts
Posted 11 August 2012 - 12:49 AM
How would I be able to loop through this table and print each variable
mytable = {}
mytable.var1 = "UNO"
mytable.var2 = 69
mytable.var3 = false
mytable.var4 = "cats"
839 posts
Location
England
Posted 11 August 2012 - 04:14 AM
Normally you assign tables as:
mytable = {}
mytable[1] = "UNO"
mytable[2]= 69
mytable[3] = false
mytable[4]= "cats"
Unless you have to use the table.var approach , I recommend assigning your table as shown above, then you can do one of the following:
-- if you know how many variables are saved:
for cnt = 1,4 do
print(mytable[cnt])
end
-- if you do not know how many variables are saved:
n = 1
while mytable[n] ~= nil do
print(mytable[n])
n = n + 1
end
I hope that helps.
8543 posts
Posted 11 August 2012 - 04:28 AM
How would I be able to loop through this table and print each variable
mytable = {}
mytable.var1 = "UNO"
mytable.var2 = 69
mytable.var3 = false
mytable.var4 = "cats"
You would do something like this:
for k, v in pairs(mytable) do
print(k.." = "..v)
end
This will loop through each key in the table (var1, var2, etc.) and print out the key name, followed by an equals sign, followed by the value. The output would look something like:
var1 = UNO
var2 = 69
var3 = false
var4 = cats
11 posts
Posted 11 August 2012 - 04:29 AM
Actually what Im hoping to do is give the variables different names as in
mytable.age,
mytable.height,
mytable.weight
And loop through them
8543 posts
Posted 11 August 2012 - 04:39 AM
Actually what Im hoping to do is give the variables different names as in
mytable.age,
mytable.height,
mytable.weight
And loop through them
The loop I posted above is probably what you're looking for. It's fairly simple. Let me know if you can't figure out how to modify it for what you're trying to do.
11 posts
Posted 11 August 2012 - 05:52 AM
THANKS!