del( table, value )
table the variable name of the table
value the data to remove from the table

This function will remove an entry from a table that matches the value given. If you have more than one entry holding the same value, only the first entry that is found to match will be removed.


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

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

Then if we want to remove a value from the table, we can do this:

del( colors, "blue" )
1 "red"
2 "pink"
3 "yellow"

Notice that "blue" was the value stored at key #2. Deleting "blue", also updated the rest of the list's keys.

This happens for tables that have numbered keys, but it does not change tables that have named keys.

Return Value

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

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

print(returned) --prints a

print(table[1]) --prints b

Images in this Guide by NerdyTeachers is licensed under CC BY-SA 4.0


5 May 2023