unique tables instances

Hello guys, I am not sure if I am losing my mind but I just want to clarify something about tables. Code below:

function setup()
local table1 = {5,3,1,2,4}
local table2 = {}

table2 = table1

print(table1[1]) --gives 1
print(table2[1]) --gives 1 but shouldn't this be 5???



WOW how did I miss that after programming on codea for a year+

Now I have to go back and check all my code for possible bugs…

Thanks for the help @Ignatz

Would the most elegant way of making copies then to just use a for loop and table.insert?

@archistudent that will work if your table is only one level deep (i.e. no tables within tables). If you have nested tables, then it may be worthwhile creating a deep copy function. http://lua-users.org/wiki/CopyTable

@epicurus101 I see , very interesting. I’ve always wondered if theres a function to return the [“keys”] from a table?

@archistudent Are you after something like this.

function setup()
    for key,value in pairs(tab) do

From StackOverflow

Given a table called tbl

local keyset={}
for k,v in pairs(tbl) do

The keys will not be in order, so they may not be in the same order as tbl. For that, you may need to create keyset a little differently


So now, if tbl[k]=v, then keyset[v] gives you the key k

You could build the keys into tbl from the start, with

tbl[k]={k,v} -- for key k and value v

@dave1707 @ignatz I will test those out. I think I meant looking for all [“keys”] for an unknown table. For example if there was a table that could be found from an http request, but I don’t know its contents, it seems ignatz’ code can list the keys?

yes. If the table doesn’t have keys, it will just give a list of numbers 1, 2, 3, 4… (because Lua has to have keys, so it just creates them in number order)

@archistudent Here’s a function to get the keys. Call the function passing the table and it will return the keys. As @Ignatz said above, any table value without a key will be given a number for the key.

function setup()
    print(table.concat(keys," "))

function getKeys(t)
    local temp={}
    for keys in pairs(t) do
    return temp

@ignatz @dave1707

Just tested it out, its very useful. Now I feel like putting my entire project data set into one table… but perhaps that would cause some havoc.

@archistudent - while you are playing with tables, note you can store functions in them too. This is very useful for creating a library of utility functions. By putting them in a table, you ensure the function and variable names won’t clash with the rest of your code.


@ignatz Oh i see, that sounds useful.

Are these the same?

table = {["key"] = {}} 
table = {key = {}}
table = {}
--is the same as:
table ={key ={}}

@yojimbo2000 I have used table = {[“key”] = {}} quite a lot in my apps, should I change them to table = {key = {}} and update them on appstore?

I havent experienced any bugs yet…

@archistudent - they are the same, but the primary use of quotation marks is where you want to define a key that can’t be used as a variable name, eg 1abc. That’s where you need to put it in quotes and inside square brackets.

@archistudent By using the form [“key”], you can also have spaces in the key.

function setup()
    tab={["k e y"]=123}
    print(tab["k e y"])

@ignatz @dave1707 I see, I think I will keep to this method then, it seems to work well for me. Cheers YK

@archistudent You’ll find that there are different ways of doing the same thing. Some ways use less keying and some ways execute faster. Pick the one that feels right for you.