Problem with finding stuff in a table

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
rokit boy
Party member
Posts: 198
Joined: Wed Jan 18, 2012 7:40 pm

Problem with finding stuff in a table

Post by rokit boy » Sat Mar 31, 2012 2:05 pm

Ok so let's say I have a table called lol and I want it to delete EVERY SINGLE INSTANCE of value
I figured out this mechanism:

Code: Select all

for i = 1, #lol, 1 do
	if lol[i] == value then
		table.remove(lol,i)
		print(i)
	end
end
I did this in Java before by doing this:

Code: Select all

for (String thing : a) {
    if (thing.equals(b)) {
    print("Found it!");
    }
}
Oh god for loops are so beautiful in Java :D

To the point:
Can somebody improve my first code so it deletes EVERYTHING with the contents of "value".

A plus tard,

rokit boy (I'm not French, but I like French)
u wot m8

User avatar
lizard
Prole
Posts: 16
Joined: Wed Aug 04, 2010 6:38 pm
Location: Thailand

Re: Problem with finding stuff in a table

Post by lizard » Sat Mar 31, 2012 2:38 pm

Do not use table.remove() with large tables if you have a large amount of elements to removal. Copy elements which should not to be removed into new table, and then remove old table.

User avatar
rokit boy
Party member
Posts: 198
Joined: Wed Jan 18, 2012 7:40 pm

Re: Problem with finding stuff in a table

Post by rokit boy » Sat Mar 31, 2012 2:41 pm

My table is not large.
The rest:

Code: Select all

value = "Hello"
lol = {"Hello","Hello","Hi"}
When I do my for loop, it only removes the first hello.
u wot m8

User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Problem with finding stuff in a table

Post by tentus » Sat Mar 31, 2012 2:46 pm

You are removing an element from a table and then continuing forwards (the second element becomes the first element, but you've already comepleted that iteration in your for loop).

Think of it like pulling steps out from a staircase, while you're climbing it. The obvious fix, then, is to walk down the staircase.

Code: Select all

function removeValFromTable(value)
	for i = #sometable, 1, -1 do
		if sometable[i] == value then
			table.remove(sometable,i)
		end
	end
end
Kurosuke needs beta testers

User avatar
rokit boy
Party member
Posts: 198
Joined: Wed Jan 18, 2012 7:40 pm

Re: Problem with finding stuff in a table

Post by rokit boy » Sat Mar 31, 2012 4:04 pm

tentus wrote:You are removing an element from a table and then continuing forwards (the second element becomes the first element, but you've already comepleted that iteration in your for loop).

Think of it like pulling steps out from a staircase, while you're climbing it. The obvious fix, then, is to walk down the staircase.

Code: Select all

function removeValFromTable(value)
	for i = #sometable, 1, -1 do
		if sometable[i] == value then
			table.remove(sometable,i)
		end
	end
end
THANK YOU! Used for my WIP crappy lib. Will give you credit. Publishing the lib in a moment.
u wot m8

Post Reply

Who is online

Users browsing this forum: No registered users and 10 guests