|
Is it possible to put a table in a table/
For example...
local tab1 = {"apple", "orange", "pear"}
local tab2 = {tab1} |
|
|
Certainly.
"tables", although data structures, are also a type of data themselves within Lua.
Thus, you can index them within another table as you would any other type of data.
An example is shown below:
Table = {["Tab"]={"Cookies"}};
print(Table.Tab[1]); This would print the string data "Cookies" because "Cookies" is numerically-indexed within the first position of the indexed table. |
|
|
local tab1 = {"apple", "orange", "pear"}
local tab2 = {unpack(tab1)} |
|
|
local t = {1,2,3}
local tt = {t,4,5,6}
local ttt = {tt,7,8,9}
print(unpack(t))
print(unpack(tt[1]),unpack(tt))
print(unpack(ttt[1][1]),unpack(ttt[1]),unpack(ttt)) |
|
|
WrathsongJoin Date: 2012-07-05 Post Count: 22393 |
|
|
|
unpack is referencing to its name.
It unpacks the table, or gets the table's contents and organizing them in the order they were stored in the table.
"Some people want it to happen, some wish it would happen, others make it happen" - MJ |
|
|
Thats not what unpack does that doesn't even make sense.
Unpack turns an array into a tuple. |
|
|
srry but erm, what's a tuple? |
|
|
So lets say that "contestants" is a table with values in it.
Could I do...
activecontestants = {unpack(contestants)} |
|
|
^ That should work perfectly fine.
"Some people want it to happen, some wish it would happen, others make it happen" - MJ |
|
|
|
Also, a tuple is an unordered set of known values with names.
So, now I see what someone above meant.
When you unpack a table like below;
local table = {1,5,8}
print(unpack(table))
The output would be,
1 5 8
It is printing a 'tuple'. A unordered set of known values with names!
"Some people want it to happen, some wish it would happen, others make it happen" - MJ |
|
|
Since we're on the topic of tuples, functions can return them too
local function thing()
return 10, 20
end
local a, b = thing()
print(a, b) --> 10 20 |
|
|
^
Did not know that! That will help me a lot.
"Some people want it to happen, some wish it would happen, others make it happen" - MJ |
|
|
Actually you can do variable assignments with tuples too
local a, b, c = 3.14, 42, 13
print(a, b, c) --> 3.14, 42, 13 |
|
|
^
I knew that; I use it all the time.
"Some people want it to happen, some wish it would happen, others make it happen" - MJ |
|
|