How can I remove a value from a table without knowing the key?

  1. What do you want to achieve? I want to delete a table from another table, or otherwise use a loop for.

  2. What is the issue? I use a “for” loop to list all tables in another table, then take a value from the table in the table, but it doesn’t work when the table in the table has a key, but it does when there is no key. But without the key, you cannot delete the table with table.remove.

  3. What solutions have you tried so far? I have searched the internet and the forum.

local MetaTable {
  [Time] = {arguments, nil} -- The "for" loop does not work
  {Time, arguments, nil} -- Can not use table.remove
}

for key, index in iparis (MetaTable) do
  -- code
end

I would be grateful for any help.

1 Like

Couldn’t you use table.find and then that returns the index then use table.remove unless I am misunderstanding in that case elaborate

1 Like

It automatically asigns a number as an index here. The following 2 examples are the same

{ {Time, arguments, nil} } -- This is the same as 
{ [1] = {Time, arguments, nil} } -- This

Another thing you can do is search for the table, with table.find(table, value)

Note, I refer to what you call “key” as “index” and for your “index” my “value”.

Table.find Can’t search table within table

local T = {
  {"222"}
  {"333"}
}
print(table.find(T, "222"))
print(table.find(T, 1))

>>> nil
>>> nil

The “For” loop does not work if there is an index ([1] = {}), and table.find cannot search for a table and returns nil:

local T = {
  {"222"}
  {"333"}
}
print(table.find(T, "222"))
print(table.find(T, 1))

>>> nil
>>> nil

I don’t know how the “for” loop worked with the index ({[1] = {}}).

Script:


Output:
SharedScreenshot2
Line 3: all values in MetaTable1, only the value with a small index was printed.
Line 7, 8: All values are printed.

The iterator function ipairs is intended for use in arrays (thus the i which stands for index). Use pairs instead.

This means that several functions of the table library break.

1 Like

Thank you very much, you helped me a lot.