Copy, not direct reference of table?

Hi Brent,

Did you see my solution above? I think I approached this with the “simple” approach you had mentioned. :slight_smile:

That link is very helpful, I need to better explore the lua users wiki! For as much as I’ve been pouring myself into Corona and Lua, I’ve not made enough use of that resource. Thanks for pointing that out! [import]uid: 105707 topic_id: 32957 reply_id: 131145[/import]

Someone correct me if I am wrong but if you are using the JSON library or don’t mind using it, a very simple way to copy a table would be the following.

[code]

require “json”

local map = {
{1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 1, 0, 0, 1},
{1, 0, 1, 0, 1, 0, 0, 1},
{1, 0, 1, 0, 0, 0, 0, 1},
{1, 0, 1, 0, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1}
}

local jCopy = json.encode(map)

local copyOfMap = {}
copyOfMap = json.decode(jCopy)

[/code] [import]uid: 56820 topic_id: 32957 reply_id: 131146[/import]

@mpappas

Good lord you whipped that up quick. I was going to give it a go and you already had this up! As they say… “Siiiiiiiiick!”

I just tried your code out. I’ve been wrestling with figuring out how to print the contents of my arrays for a long time. That print function is insanely useful for me :slight_smile:

Very neat to see that the deepCopy function works as needed. Thank you :slight_smile:

[import]uid: 105707 topic_id: 32957 reply_id: 131147[/import]

@anderoth, yes, that should work too. It should work great for light duty stuff (under a 100kb, not in loops, etc)

There’s more overhead encoding the data into/out of the json string format, but it would definitely return a new structure, and be just fine for a lot of cases.

@EHO, ehh, it was just a couple utility functions I had laying around :wink: Enjoy. [import]uid: 79933 topic_id: 32957 reply_id: 131148[/import]

@anderoth,

You’ve hit on something important for me…

I’ve not explored the save/load aspect of my project. I will need to have that capability for my map tables.

Do you use the json API or another solution for saving/loading data in your projects?
[import]uid: 105707 topic_id: 32957 reply_id: 131150[/import]

I’m asking here, why the table object is different from the string object:

in this case the string are duplicated…

-- STRING a and b are DIFFERENT objects  
a = "a"  
b = a  
a = "new a"  
print(a) -- print new a  
print(b) -- print a  

in this case the table are a copy, pointing to the same object

-- TABLE a.a and b.a are the SAME object  
a = { a = "a" }  
b = a  
a.a = "new a"  
print(a.a) -- print new a  
print(b.a) -- print new a  

do u know why this difference?

is incredible how the lua don’t have a native method to copy a table… [import]uid: 6732 topic_id: 32957 reply_id: 132118[/import]

@EHO

Sorry for not responding sooner, I just now received the email notification that you posted here. The forums are horrible lately about that.

Concerning JSON, yes I use it to save variables for games in progress. Makes it easy to save/load them when they are just strings. For other things I need to search through I use SQLite.

@PValentini

I agree a built in table copy would be a great thing, however the lua language doesn’t support one. Corona Labs would most likely not add something that doesn’t follow the lua standard. [import]uid: 56820 topic_id: 32957 reply_id: 132143[/import]

I’m asking here, why the table object is different from the string object:

in this case the string are duplicated…

-- STRING a and b are DIFFERENT objects  
a = "a"  
b = a  
a = "new a"  
print(a) -- print new a  
print(b) -- print a  

in this case the table are a copy, pointing to the same object

-- TABLE a.a and b.a are the SAME object  
a = { a = "a" }  
b = a  
a.a = "new a"  
print(a.a) -- print new a  
print(b.a) -- print new a  

do u know why this difference?

is incredible how the lua don’t have a native method to copy a table… [import]uid: 6732 topic_id: 32957 reply_id: 132118[/import]

@EHO

Sorry for not responding sooner, I just now received the email notification that you posted here. The forums are horrible lately about that.

Concerning JSON, yes I use it to save variables for games in progress. Makes it easy to save/load them when they are just strings. For other things I need to search through I use SQLite.

@PValentini

I agree a built in table copy would be a great thing, however the lua language doesn’t support one. Corona Labs would most likely not add something that doesn’t follow the lua standard. [import]uid: 56820 topic_id: 32957 reply_id: 132143[/import]

Is there any reason table.copy() from Corona’s table library wouldn’t work for @EHO in this case? I’ve had pretty good success with it. [import]uid: 27119 topic_id: 32957 reply_id: 133370[/import]

@drewns

I’m not sure that function will work with an array of arrays. That’s my sense on reading about it but will have to try it out maybe for curiosities sake.
http://developer.coronalabs.com/reference/index/tablecopy

I’m currently using the function that mpappas shared. It seems to work great for what I need.

I generally like Lua quite well although I think it’s odd that there’s not a built in way to manage this. [import]uid: 105707 topic_id: 32957 reply_id: 133404[/import]

Is there any reason table.copy() from Corona’s table library wouldn’t work for @EHO in this case? I’ve had pretty good success with it. [import]uid: 27119 topic_id: 32957 reply_id: 133370[/import]

@drewns

I’m not sure that function will work with an array of arrays. That’s my sense on reading about it but will have to try it out maybe for curiosities sake.
http://developer.coronalabs.com/reference/index/tablecopy

I’m currently using the function that mpappas shared. It seems to work great for what I need.

I generally like Lua quite well although I think it’s odd that there’s not a built in way to manage this. [import]uid: 105707 topic_id: 32957 reply_id: 133404[/import]