Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
removing sprites from tables with a for — Gideros Forum

removing sprites from tables with a for

ricvieiraricvieira Member
edited October 2013 in General questions
hi all, just a quick question, I have a table with some sprites there, called birdList, if I iterate it from back to front, I can iterate it all and remove all like this:
count= #birdsList
for i=count, 1,-1 do
table.remove(birdsList, i)
end
but the same list, if i iterate it from start to end i get an error, that one of them, the 6th is nil (i have 10 sprites there)
count= #birdsList
for i=1, count do
table.remove(birdsList, i)
end
I've searched the forum for an answer to this behaviour in the for cycle, i couldnt find any, can someone explain to me whats happening

tnx in advance and sry about the noob question :)

Comments

  • ar2rsawseenar2rsawseen Maintainer
    edited October 2013 Accepted Answer
    Yes the problem is that once you remove the element from table, tables count becomes lesser than it is defined in your loop

    The solution is to loop backwards
    for i=#birdsList,1,-1 do
        table.remove(birdsList, i)
    end
    or control loop counter:
    local i=1
    while i <= #birdsList do
            table.remove(birdsList, i)
    end
    hope that helps ;)
  • yeah, it helps a lot, tnx

    I thought I was controling the loop counter, with the
    count= #birdsList
    for i=1, count do
    but from what you're saying is the variable "count" is being changed by removing an item from the list, even if its outside the for cycle, right?

    again, sry about the noob questions, but im writting this down so I wont forget lol, lua and its global scope is very different from what Im used to programming ;)

    tnx again
  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    No count remains the same, while the real size of table is getting smaller, so you finally reach the index of element that does not exist anymore, because you are using old size of table :)
  • OZAppsOZApps Guru
    Accepted Answer
    @ricvieira,
     tbl = {} -- has 0 elements
     table.insert(tbl, "ONE") -- has 1 element
     table.insert(tbl, "TWO") -- has 2 elements
     table.insert(tbl, "THREE") -- has 3 elements
     
     --[[
    Now the tbl has 3 elements and they are like an index, which is dynamic, so there is no guarantee that the element at 1 will be 1, unlike how it is with VB or Java.
     
    so when you use table.remove(tbl, 2) , yes element 2 is removed and... HERE's the important bit,
     
    tbl[1] is still "ONE"
    and tbl[2] is now "TWO"
    and there is no tbl[3] 
     
    but you would ask what happened to tbl[3], I removed tbl[2] not [3]. That is the reason *why* a reverse loop is used, so that when the table dynamically reassigns the indexes, you do not encounter an error
    --]]
     
    do if you used
    for i=#tbl, 1, -1 do
     table.remove(tbl, i)
    end
     
    -- it will work perfectly fine, but if you had it the other way... errors
     
    -- NOTE: that if the table hasd key value pairs, they would not get reassigned automatically and #tbl would not include them in the result.
    If you need to spend a little more time understanding Lua, try @Ar2rsawseens's site or my blogs at http://howto.oz-apps.com and http://learnlua.oz-apps.com

    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
  • ricvieiraricvieira Member
    edited October 2013
    tnx ozapps and yeah, ive been using your blog and ar2rsawseen site, great sites by the way, congrats :) just sometimes, small details arent on the tutorials, thats why i asked here

    tnx again :)

    -- NOTE: that if the table hasd key value pairs, they would not get reassigned automatically and #tbl would not include them in the result.
    yep, thats what was happening to me, its a table with key value pairs, it was a noob mistake lol
Sign In or Register to comment.