suppose you have a table
t = {"a", "b", "c", "d", "e"}
and you can pick a random value and remove it from table t by
local object = table.remove(t, math.random(#t))
but when you have a object,for example, "b", and want to get its index in table t and remove it from t,is there any effect way
to find the value "b" in table t(not iterate over the elements)?
Just like the these functions in Sprite
Sprite:removeChild(child) ---look up by item
Sprite:removeChildAt(index) ----lookup by index
Sprite:getChildAt(index)
Sprite:getChildIndex(child)
Comments
Another point is that "b" is a string, where as the other values might not be a string or a number, but a table (object/userdata, etc).
Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
Cool Vizify Profile at https://www.vizify.com/oz-apps
I think you might be out of luck, reading the LUA wiki on tables gives this little snippet at the bottom of this page:
Note that table.remove only works with numeric indexes. For dictionaries you can just unset tables entries with tablevariable["index"] = nil
Doh!!!
Ninja'd by @OZApps
Website: http://www.castlegateinteractive.com
https://play.google.com/store/apps/developer?id=Castlegate+Interactive
https://sites.google.com/site/xraystudiogame
https://sites.google.com/site/xraystudiogame
Website: http://www.castlegateinteractive.com
https://play.google.com/store/apps/developer?id=Castlegate+Interactive
For example, in this case, you can set up an index table, so if you have table t = {"a", "b", "c", "d", "e"}
You can create
https://sites.google.com/site/xraystudiogame
Website: http://www.castlegateinteractive.com
https://play.google.com/store/apps/developer?id=Castlegate+Interactive
Note that table.remove only works with numeric indexes.
https://sites.google.com/site/xraystudiogame
I am not sure what you are trying to do, maybe you do not have to have two tables, you can access the entire table as an array, so all of table.xxx functions work. The data that you store can be structured a little better maybe with fields, or something that will help you ID the data. Dunno if you have heard of bitmap indexes, if that is going to be helpful (if you do go with non numeric indexes)
Likes: amin13a
Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
Cool Vizify Profile at https://www.vizify.com/oz-apps
I just want to give up, I have to accept to get the value by iterating over the elements in a numeric indexed table. It performs just ok for a 9X9 table in my new game.
https://sites.google.com/site/xraystudiogame