DELI


deli( table, index )
table the variable name of the table
index the numbered key to remove from the table

This function will remove an entry from a table that matches the index given. This only works for tables that have numbered keys, but it does not change tables that have named keys.



We can create a table that is a list of color strings first and insert values immediately like this:

colors = { "red", "blue", "pink", "yellow" }
Colors
1 "red"
2 "blue"
3 "pink"
4 "yellow"

These values are automatically indexed (given numbered keys) so we can use those indexes to remove an entry like this:

deli( colors, 2 )
Colors
1 "red"
2 "pink"
3 "yellow"

Notice that "blue" was the value stored at index 2. Deleting "blue", also updated the rest of the list's keys so that there isn't a gap at index 2.



Return Value

This function will return the deleted item. If the function did not delete anything, then it will return nil.

table = { "a", "b", "c" }
returned = deli(table,1)

print(returned) --prints a

print(table[1]) --prints b

518

5 May 2023

Font