Search code examples
luanullkeylua-tableunset

Lua key returning item from list as nil


I have a list in lua that looks something like this:

list = {item1, item2, item3, etc}

At some point in my code, I set one of these items to nil by doing this:

list[2] = nil

Now, my problem is that whenever I try and get that value, it gets returned as nil, when I want it to return the next value in the list. For example:

list[2] == nil

instead of:

list[2] == item3

Thanks for all the help!


Solution

  • You should instead use table.remove call.

    table.remove(list, 2)