jonasan [Lexaloffle Blog Feed]https://www.lexaloffle.com/bbs/?uid=19104 Deleting objects from tables by index... <p>Hey All,</p> <p>First post here after recently picking up and playing with pico-8 (humble bundle). It's rather brilliant I have to say!</p> <p>I have only been playing around with coding for a couple of months and remain very much a novice, and this is my first time using lua with pico-8.</p> <p>My question is regarding tables of tables (objects I am spawning in my game), and the deletion of those objects...</p> <p>Let's say we have the following little program which spawns 5 THINGS, gives them unique names, and then adds them to the list a{}. We then delete the item with NAME=3, and then print some output...</p> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre> a = {} for i=1,5 do local thing = {} thing.name = i add(a,thing) end for thing in all(a) do if thing.name == 3 then del(a,thing) end end for thing in all(a) do print(thing.name) end print(&quot;# = &quot;..#a) print(a[3]) </pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>Output is: 1, 2, 4, 5, # = 4, TABLE</p> <p>This works fine, and is the standard practice I have been using to remove objects from the master table. </p> <p>However, let's say I already know which item on the main table (a) that I want to delete by its index, say the third item... i.e. a[3]</p> <p>What I'd like to be able to do is delete that sub-table from the list without iterating through the list a{} to find it (imagine a{} contains lots of objects!), and instead just delete it directly, i.e.</p> <p>del(a,[3])</p> <p>but the del() function does not work in this way, so I have been trying to set the sub-table to NIL by its index in the main table a{} i.e....</p> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre> -- REPLACE THIS -- for thing in all(a) do -- if thing.name == 3 then -- del(a,thing) -- end --end -- WITH a[3]=NIL </pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>I don't have any idea if this is the right way to approach this, but it has been producing some confusing results....</p> <p>a[3]=NIL</p> <p>produces... 1, 2, 4, 5, #=5, NIL</p> <p>which as far as I can tell means the third item in a{} is now a non-item (NIL) and will not be processed when I iterate through a{}, and the table a{} still has 5 items.</p> <p>This is not exactly what I'm looking for, as I was hoping to see the # in a{} go down to 4.</p> <p>Also.... and this is where I'm really confused... attempting to remove the first or second item, a[1] or a[2], produces similar results.... BUT if I try a[5]=nil, then the output is:</p> <p>1, 2, 3, 4, #=4, TABLE</p> <p>... which appears to have done what I wanted??</p> <p>BUT if I try a[4]=nil .... then I get:</p> <p>1, 2, 3, #=3, TABLE</p> <p>which seems to have removed both entries 4 and 5 in the table completely ?!?</p> <p>So.... I'm guessing there must be a better way to achieve what I'm trying to do that I'm not aware of, so if anyone can tell me what that is that would be great!</p> <p>...and if anyone can explain what is going on to create the strange results above then .... :)</p> <p>Sorry if that was a little long-winded, just wanted to try and be clear.</p> <p>Any input here would be much appreciated.<br /> Thanks!</p> https://www.lexaloffle.com/bbs/?tid=28507 https://www.lexaloffle.com/bbs/?tid=28507 Mon, 09 Jan 2017 09:13:14 UTC