Home > Mobile >  Lua table - When a lower index is removed the higher index all moved down 1 place to fill the spot
Lua table - When a lower index is removed the higher index all moved down 1 place to fill the spot

Time:03-28

Using Lua used is 5.1

I'm trying to get a dictionary table and store it in a table where i have a key index and the value being the data, something like:

tParams = {
    {1st table data...}  -- this one is key index 1
    {2nd table data...}  -- 2nd index
    ...
}

I'm using this code to store it:

tParams[index] = i     --i being a dictionary table

I can add the code in the right index and the table is output if called, so all fine

1   table: 0xa4a2fc0
2   table: 0x9f7c4c0

Now the problem is if I remove for example the first index data, i DON'T want the 2nd index data to move to the 1st position just because it's empty it just takes it place and becomes index 1... how can I avoid that?

Or if you have another solution in general, what i wanna do is store dictionary table data i'm getting, and if called, to be able to get the data and work with it and if i don't need them anymore to remove them.

There can be many multiple dictionaries depending on how many of the instances are created so i can't just pre make them. They will be dynamic basically... and every time they have a specific id they come with so 2 dictionaries will not have the same id.

If a dictionary is removed, the other dictionaries should not move and stay in the same index/id so they can be called and be the right data.

I hope i was clear, also please don't suggest any supplementary modules.

Thanks!

CodePudding user response:

You're not providing all the relevant code, but the basic way to remove an item from a table is tParams[index] = nil. That should not cause the problem you're describing.

It sounds as if you're using table.remove. That function is used for maintaining a proper sequence, so that the # operator and some standard library functions can be meaningful for it.

  • Related