What do you want to achieve? My for loop should run for all elements in my table.
What is the issue? My for loop only runs 1 time even tho the table has 2 elements.
What solutions have you tried so far? None, I don’t see anything wrong.
local operators = {"+","-"}
local out = {"/"}
print("B OPERATORS")
print(operators)
for index, token in operators do
print("B: "..index)
table.insert(out, token)
table.remove(operators, index)
end
print("C OPERATORS")
print(operators)
print("B OPERATORS")
print(operators)
for index, token in pairs(operators) do
print("B: "..index)
table.insert(out, token)
table.remove(operators, index)
end
print("C OPERATORS")
print(operators)
local operators = {"+","-"}
local out = {}
local function AlterTable(operators, out)
local Table = operators
for i, v in pairs(Table) do
table.insert(out, v)
table.remove(operators, i)
end
end
AlterTable(operators, out)
print(operators)
print(out)
I want to go through all of the elements of operators and put them into out if they pass an if statement (removed from the code due to not producing the error)
I also want the transfered element to be removed from operators.
local function Tbl(Table)
local Table1, Table2 = Table, {}
for i, v in pairs(Table1) do
if "Condition" == "Condition" then
table.remove(Table, i)
table.insert(Table2, v)
end
end
return Table, Table2
end
Table2 or out isn’t empty before the code runs.
I have 2 for loops.
One to move * and / to out.
Then one to move the rest to out.
Out should be {,/,,+,-} for example
The problem is even without if statement, only 1 element of the 2 elements in operators gets removed from operators and moved to out.
local function Tbl(Table1, Table2)
local Table = table.clone(Table1)
for i, v in pairs(Table) do
table.remove(Table1, table.find(Table1, v))
table.insert(Table2, v)
end
return Table1, Table2
end
print(Tbl({"A", "B", "C"}, {}))
your code was not running as you expected because
after this line, #operators would be 1 and the loop would stop because index == #operators
what I did is I looped through a temp table instead of operators so that we can access every element of the table and not just end the loop when index == #operators as we won’t remove/insert any element into the temp table, it should access every element. And ig that specifies why I used table.clone().