Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How to use table.remove

in Questions Posts: 28

In my code, I have two lists initialized like this:
list1 = {}
list2 = {}

These lists store references to objects. At some point, I want to add the first x number of references from list1 to list2, and at the same time remove those references from list1, as if I were popping them from a stack, so that the second item in list1 becomes the first item, the third becomes the second, etc. It looks like table.remove will do this, but I can't make sense of the syntax from the documentation. Would my code look like this:

for i = 1, x do
list2[i] = list1.remove(1)
end

Or like this:

for i = 1, x do
list2[i] = table.remove(list1, 1)
end

Or are both of these wrong?

Comments

  • edited March 5 Posts: 1,979

    The second one is correct (not that I've tested it). You might want some check that list1 and list2 are at least x in length.

    Have you tried the official Lua manual? Might be a bit clearer:

    https://www.lua.org/manual/5.3/manual.html#pdf-table.remove

  • Posts: 28

    It turns out the second way is the right way. I should have just tried it before I posted!

  • dave1707dave1707 Mod
    edited March 5 Posts: 5,810

    @Eustace You shouldn't use table.remove in a for loop unless you go thru the table in reverse order. If you do a remove in a for loop, when the remove happens, everything in list1is shifted down. So if you remove the first entry in the for loop, entry 2 is now entry 1, 3 is 2, etc. When the for loop increments to 2, you are referencing position 2 which is the original entry 3 in list1 because list1 has entry 2 in position 1 and entry 3 in position 2, etc.

    EDIT: They way you're using the code in the second one, then what I'm saying above isn't true in that case since you're always moving the first entry from list1 to list2. But if you're doing a for loop and reading an entry, then deleting it, then what I explain above would cause a problem.

  • Posts: 28

    dave1707, I'm using the increment variable i only to place the new references into list2. Each time thru the for loop I remove the first element from list1. I want list1 to act like a stack, so I am popping object references from it. How is this a problem? Is there something I'm missing?

    yojimbo2000, I am doing the length check before this point.

  • dave1707dave1707 Mod
    Posts: 5,810

    @Eustace What you're doing is OK. You're always removing the 1st element in the table. Here is an example of what I was referring to. Looking at the code you would think that I'm moving the entries 1,2,3,4,5 from table t1 to table t2 and removing the entries 1,2,3,4,5 from table t1. But that's not what happens.

    function setup()
        t1={1,2,3,4,5,6,7,8,9,10}
        t2={}
    
        for z=1,5 do
            t2[z]=table.remove(t1,z)
        end
    
        print("table 1...",table.concat(t1," "))
        print("table 2...",table.concat(t2," "))
    end
    
Sign In or Register to comment.