this is the line : username = {"user1", "user2", "user3", "user4"}
after this i try to print the variable( print(username) )but all it shows is "table: 15d69505" when i run it.
Am I doing something wrong or does this just not work?
thanks,
kingcoon
print(username[1])
print(username[2])
print(username[3])
print(username[4])
--or
for x=1, #username do
print(username[x])
end
Acctually, tables start at 0
Acctually, tables start at 0
The {} make it a table and the data is stored in indicies of the table. The way you have setup the table it will use standard number indicies, starting with the number 1 and increasing with each additional data set.
Here are a couple of ways to print the data.print(username[1]) print(username[2]) print(username[3]) print(username[4]) --or for x=1, #username do print(username[x]) end
local t = {"firstEntry", "secondEntry"}
t[-1] = "negativeFirstEntry"
print(t[-1])
It will output:
negativeFirstEntry
local t = {x = 5, y = 1}
print(t.x .. ' ' .. t.y)
Will display:
5 1
local t = {x = 5, y = 1}
print(t[1] .. ' ' .. t[2])
Will throw an error because you're trying to concatenate the string ' ' with t[1] and t[2], which are nil values.Acctually, tables start at 0The {} make it a table and the data is stored in indicies of the table. The way you have setup the table it will use standard number indicies, starting with the number 1 and increasing with each additional data set.
Here are a couple of ways to print the data.print(username[1]) print(username[2]) print(username[3]) print(username[4]) --or for x=1, #username do print(username[x]) end
When you create a new table in Lua the indexing starts at 1 and increases with every entry.
However, you can assign a value to any numeric; string; or actual identifier as an index.
Consider the following code:It will output:local t = {"firstEntry", "secondEntry"} t[-1] = "negativeFirstEntry" print(t[-1])
negativeFirstEntry
Also:Will display:local t = {x = 5, y = 1} print(t.x .. ' ' .. t.y)
5 1
However, the following code:Will throw an error because you're trying to concatenate the string ' ' with t[1] and t[2], which are nil values.local t = {x = 5, y = 1} print(t[1] .. ' ' .. t[2])
Also, luanub, it's usually best to iterate through tables using an iterator function such as 'pairs' or 'ipairs', but it doesn't usually matter all that much.