Table remove

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
tdc5013
Prole
Posts: 38
Joined: Wed Feb 08, 2012 4:24 pm

Table remove

Post by tdc5013 »

Hi,

I'm trying to compare all entries of a table against all entries of another, any identical entries then being removed from the second table. The problem is a for loop will only let me check whatever entries the iterator happens to correspond to at that moment. I'm looking to compare every single entry of both tables against one another.

Apologies if this is really simple, but it's way past my bedtime and this has me stumped.

Thanks in advance!
User avatar
substitute541
Party member
Posts: 484
Joined: Fri Aug 24, 2012 9:04 am
Location: Southern Leyte, Visayas, Philippines
Contact:

Re: Table remove

Post by substitute541 »

What about a double for-loop?

Code: Select all

for i=1, #table1 do
    partA = table1[i]
    for j=1, #table2 do
        partB = table2[j]
        if partA == partB then table.remove(table2, j) end
    end
end
If someone haven't noticed, that's almost the same as my collision-detection loop, minus the optimization since both tables might not have the same number of indices.
Currently designing themes for WordPress.

Sometimes lurks around the forum.
tdc5013
Prole
Posts: 38
Joined: Wed Feb 08, 2012 4:24 pm

Re: Table remove

Post by tdc5013 »

Thanks for the quick reply.

Just to make sure I understand I'll get more specific as to what my problem is:

I have two tables filled with objects that may or may not have the same index value at any given time. The objects are cards with a suit and number attribute.
The check will be if the Suit and Number of the entry in the first table is identical to that of the second then remove said entry from the second table.

Code: Select all


for i = 1, #table1 do
	for j = 1, #table2 do
		if( table1[i].n  == table[j].n and table1[i].s == table2[j].s ) then
			table.remove(table2, j)
		end
	end
end	

Will this code do the trick?
Thanks again for the help.
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Table remove

Post by Taehl »

Since you're removing things, I highly recommend iterating backwards so that table.remove won't mess you up. Like so:

Code: Select all

for i=#table1, 1, -1 do
   for j=#table2, 1, -1 do    -- backwards!
      if( table1[i].n  == table[j].n and table1[i].s == table2[j].s ) then
         table.remove(table2, j)
      end
   end
end
Otherwise you may end up skipping entries, and getting other subtly wrong behavior.
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+.
tdc5013
Prole
Posts: 38
Joined: Wed Feb 08, 2012 4:24 pm

Re: Table remove

Post by tdc5013 »

That's great advice, thank you.

While I'm on the subject of for loops I may as well ask: If I return a table entry 'i' during a for loop will that automatically end the loop? Or will it keep on looping and returning until it's defined stopping point?
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Table remove

Post by Taehl »

Using a "return" will take you out of whatever function you're in. If you want to exit just the innermost loop, you can use "break". For further reading, check out http://www.lua.org/manual/5.1/manual.html#2.4.4.
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+.
tdc5013
Prole
Posts: 38
Joined: Wed Feb 08, 2012 4:24 pm

Re: Table remove

Post by tdc5013 »

I'll take a look.

Thanks for your help, it was really useful!
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 3 guests