Problems using "table.remove()"

Let’s say I have this simple table:

local t = { apple, orange, grape }

I want to remove"apple" from the table entirely. However, according to my research, table.remove() can only be used with an index value, as follows: table.remove( t, 1 )

So, I can remove the entry at index 1, which happens to be “apple”, but that’s not my goal. I want to remove the entry “apple” no matter where it lies in the index sorting. If “apple” happens to be at index 2, or index 47, or any index, I want to remove it.

The only method I’ve found (so far) is looping over the entire table using a for i,v in ipairs routine, with an IF statement nested within checking if the next iteration in the loop happens to be “apple”… and if that’s true (v=“apple”) then use table.remove(t,i) to remove the entry at that index position (i). This method works, but it seems clumsy and unnecessary. I’m hoping that Lua features some method to simply delete a table value by name , not just by index number.

Any help on this issue please? I’m also aware of key-value tables, i.e. { apple=“1”, orange=“2”, grape=“0” }, but I’m hoping to avoid using this structure for my current purpose, as it adds varying complications (i.e. not easily being able to count the total number of key-value pairs using the “#t” notation (at least I have not found a method for that)).

Brent
[import]uid: 9747 topic_id: 3014 reply_id: 303014[/import]

http://developer.anscamobile.com/code/useful-functions-tables-and-arrays
Edit: Doesn’t actually seem to help directly, but you can do:

[lua] t=TableUtils.select(t, “value ~= apple”)[/lua]
[import]uid: 7356 topic_id: 3014 reply_id: 8789[/import]

Thanks as always Magenda!

I just wrote my own tiny module based on the much larger “tableUtils.lua” and included it in my project. It does the same thing as I detailed in my original post, but it helps “clean up” my main.lua code a little bit, having the function packaged outside the main file.

Brent
[import]uid: 9747 topic_id: 3014 reply_id: 8799[/import]

Simplicity is key. :slight_smile:

[lua]t.apple = nil[/lua]

That will “delete” the key apple from your table. [import]uid: 10479 topic_id: 3014 reply_id: 8807[/import]

Steffen, what about the case when you have “apple” as a *value* and you don’t know the entry’s index? Is there anything else you can do, except cycling and checking with a generic for loop (pairs) to find the index and delete it ? [import]uid: 7356 topic_id: 3014 reply_id: 8839[/import]

You mean something like this:
[lua]t = { 1, 2, 3, 4, x = 5, unknownKey = “apple”, y = 6 }[/lua]

You are correct, to find the key with the value “apple” you do have to walk through the table via pairs until you find the “apple”.

[lua]t = { 1, 2, 3, 4, x = 5, unknownKey = “apple”, y = 6 }
for key, value in pairs(t) do
if value == “apple” then
print(key)
break
end
end[/lua]

If you need to do this often, you can cache the key:

[lua]t = { 1, 2, 3, 4, x = 5, unknownKey = “apple”, y = 6 }
for key, value in pairs(t) do
if value == “apple” then
t.keyForApple = key
print(key)
break
end
end

– get “apple” via the keyForApple entry containing the key which points to the entry for “apple”
myApple = t[t.keyForApple][/lua]
[import]uid: 10479 topic_id: 3014 reply_id: 8840[/import]

I would recommend having another table as the reverse lookup for a table. Basically, an indexing table:

main_table = { bob=“sam”, biff=“moon”, rar = “stuffit” }

main_table_index = { sam = “bob”, biff=“moon”, stuffit = “rar” }

So you can quickly do reverse lookups later. [import]uid: 8541 topic_id: 3014 reply_id: 8975[/import]