For loop only runs once even tho the table has 2 elements

  1. What do you want to achieve? My for loop should run for all elements in my table.

  2. What is the issue? My for loop only runs 1 time even tho the table has 2 elements.

  3. 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)

Output:

  11:36:06.310 B OPERATORS
  11:36:06.310   ▼  {
                    [1] = "+",
                    [2] = "-"
                 }
  11:36:06.310  B: 1
  11:36:06.310  C OPERATORS
  11:36:06.310   ▼  {
                    [1] = "-"
                 }

You need to use an Iterator to loop through tables
Try this instead:

for index, token in pairs(operators) do

I guess it’s because you haven’t used Pairs() here, replace it with for index, token in pairs(operators) do and it should work fine

I thought it would automatically use the better one.
I will try that.

Updated code:

	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)

Output:

11:46:34.817  B OPERATORS
  11:46:34.817   ▼  {
                    [1] = "+",
                    [2] = "-"
                 }
  11:46:34.817  B: 1
  11:46:34.817  C OPERATORS
  11:46:34.818   ▼  {
                    [1] = "-"
                 }  

ipairs also doesn’t work.

try this code instead

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

What did you change? And why?
Chars

Code:

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)

Output:

  11:56:14.070   ▼  {
                    [1] = "-"
                 }  -  Server - A:11
  11:56:14.070   ▼  {
                    [1] = "+"
                 }  -  Server - A:12

can you say me what do you want to achieve so that I can give a better solution ?

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.

this should do that work

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.

try this code

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

1 Like

How did you work around that?
Is that why the table.clone is there?

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().

1 Like

Thank you for the good explanation.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.