Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
any idea on lookup table item both by index and item — Gideros Forum

any idea on lookup table item both by index and item

alexzhengalexzheng Guru
edited May 2012 in General questions
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)

Likes: duks

+1 -1 (+1 / -0 )Share on Facebook

Comments

  • I guess that there will be some iteration, if it is supported behind the scenes in C, then you do not get to see it, otherwise you can do the same in Lua. In short, you will need to iterate through to find the value by the item.
    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).
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    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
  • ScouserScouser Guru
    edited May 2012
    @alexzheng:
    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 :(
  • yes, iterate through the table is the straight way to find the item,but it's not very effect for large table, so I'm Curious about if there are some magic code in gideros.
  • @alexzheng:
    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 :(
    By use dictionaries,how to randomly pick an item from the table?

  • ScouserScouser Guru
    edited May 2012
    I may be wrong but I think their term dictionary is used to describe a table of Key : Value pairs.
  • ar2rsawseenar2rsawseen Maintainer
    edited May 2012
    Well in every aspect there is a balance between memory usage and performance.

    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
    tIndex = {}
    tIndex["a"] = 1
    tIndex["b"] = 2
    tIndex["c"] = 3
    tIndex["d"] = 4
    tIndex["e"] = 5
    Then you can easily remove random values:
    local randomObject = math.random(#t)
    table.remove(t, randomObject)
    tIndex[randomObject] = nil
    And you can easily check if object exists and retrieve it's value:
    function checkObject(obj)
        if(tIndex[obj] ~= nil) then
             return t[tIndex[obj]]
        end
    end
    But as you've guessed, you'll probably use twice as much memory
  • alexzhengalexzheng Guru
    edited May 2012
    I have also considered use two tables exactly as you depicted, but after you perfrom table.remove, the remaining elements are reindexed, so the index stored in another table will become out of date.
  • @alexzheng: Surely then you could use something like
    local randomObject = math.random(#t)
    table.remove(t, randomObject)
    table.remove(tIndex, randomObject)
    which should keep the indices ticking over quite nicely..
  • @alexzheng: Surely then you could use something like
    local randomObject = math.random(#t)
    table.remove(t, randomObject)
    table.remove(tIndex, randomObject)
    which should keep the indices ticking over quite nicely..
    As you said above:
    Note that table.remove only works with numeric indexes.
  • @alex,
    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

    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    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
    +1 -1 (+1 / -0 )Share on Facebook
  • Thanks all.
    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.
Sign In or Register to comment.