So I'm putting brick name into a table for this thing I'm scripting. I'm not sure how to get the position so that I can use table.remove I would love to just use code like this
1 | table.remove(SelectedBricks, Brick.Name) |
If possible, Also. SelectedBricks is the name of the table. The only thing I can "search" the table by is the Brick.Name
There is no built in function to find the place of an object inside of a table.
Lua is missing a few such useful functions that most languages provide by default, I'm not sure why.
In any case, defining a function ourselves to do this is easy:
1 | function indexOf(tab, value) |
2 | for key, v in pairs (tab) do |
3 | if v = = value then |
4 | return key; |
5 | end |
6 | end |
7 | end |
This is one possible definition of your table. Note that it only returns the first instance of a value in your table, and not all of them.
In addition, it searches non-numeric keys as well, which may not be what you want.
This would be used as:
1 | table.remove(tab, indexOf(tab, Brick.Name)) |
to remove Brick.Name
from tab
.