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!
Table remove
- substitute541
- Party member
- Posts: 484
- Joined: Fri Aug 24, 2012 9:04 am
- Location: Southern Leyte, Visayas, Philippines
- Contact:
Re: Table remove
What about a double for-loop?
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.
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
Currently designing themes for WordPress.
Sometimes lurks around the forum.
Sometimes lurks around the forum.
Re: Table remove
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.
Will this code do the trick?
Thanks again for the help.
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
Thanks again for the help.
- Taehl
- Dreaming in associative arrays
- Posts: 1025
- Joined: Mon Jan 11, 2010 5:07 am
- Location: CA, USA
- Contact:
Re: Table remove
Since you're removing things, I highly recommend iterating backwards so that table.remove won't mess you up. Like so:
Otherwise you may end up skipping entries, and getting other subtly wrong behavior.
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
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+.
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
Re: Table remove
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?
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?
- Taehl
- Dreaming in associative arrays
- Posts: 1025
- Joined: Mon Jan 11, 2010 5:07 am
- Location: CA, USA
- Contact:
Re: Table remove
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+.
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
Re: Table remove
I'll take a look.
Thanks for your help, it was really useful!
Thanks for your help, it was really useful!
Who is online
Users browsing this forum: Bing [Bot] and 3 guests