Reputation: 1330
The following code:
myList = {1, 5, 9, 10}
myList[8] = 8
table.remove(myList,8)
Should produce a table with a border at 4, nil
in keys 5 and 6, and 8
at key 8, then delete the 8. This program works.
However, the following:
myList = {1, 5, 9, 10}
myList[9] = 8
table.remove(myList,9)
does not work, saying that the call to table.remove
is out of bounds!
This is contradictory: myList[9]
is a non-null entry, and if table.remove
requires that there are no borders before the given entry, then the border at 4 should have caused the first example to fail too. The only difference is that the gap in the keys is one step wider!
What's happening here and is it something I can correct?
Upvotes: 1
Views: 83
Reputation: 473272
The part of a table that is an array (integer indices starting from 1 and ending at the first nil
value) is the only part for which table.remove
is valid. The array part cannot contain "holes"; the first "hole" represents the end of the array.
If you give table.remove
indices that are not in the array part of the table, then you get undefined behavior. Maybe sometimes it will "work", and sometimes it won't. The workaround is to not specify indices outside of the array part of the table.
Upvotes: 3