Not possible without restructuring the table as an array or using another array to establish an order.
local Table = {
["Three"] = "Placeholder3";
["Two"] = "Placeholder2";
["One"] = "Placeholder1";
["Four"] = "Placeholder4";
["Five"] = "Placeholder5";
}
local SpelledOutNumbers = {"One", "Two", "Three", "Four", "Five"}
for i, word in SpelledOutNumbers do
print(Table[word])
end
Mentioning your use case would help us to possible give some better tailored suggestions.
Oh, so the table might have ‘holes’ (figuratively because it’s a dictionary with unspecified order). In that case the method in my previous post is a viable option, but it needs a small check to make sure that the key exists. The order remains.
for i, word in SpelledOutNumbers do
if Table[word] then
-- Now we know the key exists
print(Table[word])
end
end
In the latter case where Table is an array full of tables, you could take another approach and leave the keys to exist at all times, but add and remove values in the tables.
Example:
local Table = {
[1] = {Number = "One", Value = "Something"};
[2] = {Number = "Two", Value = nil};
[3] = {Number = "Three", Value = "Something"};
}
for i, array in Table do
if array.Value then
print(i, array.Value)
end
end