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]