Move item from one table to another properly?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Move item from one table to another properly?

Post by Jasoco »

Is there a better way to move an entire table entry from one table to another than deleting the first and recreating it in the second?

By which I mean everything under the table entry. Every key value pair.

Code: Select all

table1 = {
  item1 = { stuff }
}

table2 = {
}
Move item1 from table1 to table2?
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Move item from one table to another properly?

Post by ivan »

Here's a neat trick which you might find useful:

Code: Select all

local a = { 1, 2, 3, 4, 5, 6, }

local b = { unpack(table1) }
It copies all of the values from table a into table b.
I don't think it would work if you want to copy over the table keys though.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Move item from one table to another properly?

Post by Robin »

So you want the situation to go from

Code: Select all

    table1 = {
      item1 = { stuff }
    }

    table2 = {
    }
to

Code: Select all

    table1 = {
    }

    table2 = {
      item1 = { stuff }
    }
?

Then why not do

Code: Select all

table2.item1 = table1.item1
table1.item1 = nil
Help us help you: attach a .love.
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Move item from one table to another properly?

Post by Jasoco »

Robin wrote:So you want the situation to go from

Code: Select all

    table1 = {
      item1 = { stuff }
    }

    table2 = {
    }
to

Code: Select all

    table1 = {
    }

    table2 = {
      item1 = { stuff }
    }
?

Then why not do

Code: Select all

table2.item1 = table1.item1
table1.item1 = nil
Will that transfer every aspect of the original item1 including all nested tables contained therein? What about any functions that might also be contained or images, canvases, whatever that may or may not also be included as part of the table?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Move item from one table to another properly?

Post by Robin »

Yes.

In Lua, everything is basically references, so doing a = b will make a point to the same object as b.
Help us help you: attach a .love.
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Move item from one table to another properly?

Post by Taehl »

And if you do that, and change B, then A would be changed too! Took me a while to wrap my head around that when starting out in Lua. Can cause some subtle bugs if you ever assume it'll behave otherwise.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
pauloborges
Prole
Posts: 3
Joined: Tue Dec 27, 2011 5:27 pm
Location: Brazil
Contact:

Re: Move item from one table to another properly?

Post by pauloborges »

I dunno if this is what you want, but I think you can transfer all (key, value) pairs from one table to another doing this
(at least is scalable):

Code: Select all

table1 = {
	key = 'value', --string key
	[2] = 123, -- number key
	-- etc
}

table2 = {}

for key,value in pairs(table1) do
	table2[key] = value
end

table1 = {} -- or nil, depends of what you want
another great thing in Lua is the facility to swap variables:

Code: Select all

table1 = { ... }
table2 = { ... }

table1, table2 = table2, table1
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Amazon [Bot], Google [Bot] and 8 guests