if #subWords>0 then
print("Q3")
for a,b in pairs(subWords) do
table.insert(output,b)
print("Q2: Adding "..b)
end
end
----Definitions
--subWords is a table with four strings in it
--output is a table, with strings in it
----Goal
--To merge the tables "output" and "subWords"
----Output
--Repeatedly prints "Q2: Adding (word)" (after other stuff)
--Cra开发者_开发百科shes ROBLOX
Any ideas?
See Phrogz comment: subWords and output are the same table.
Note that in Lua, tables values are references, so:
t1 = {}
t2 = t1
t3 = t2
t4 = t3
// all four of these variables refer to the *same* table
print(t4.food) -- nil
t1.food = "donut"
print(t4.food) -- donut
What's happening with your code is that subWords
and output
are referencing the same table. As you loop through the items in this table you're adding new items to it, so the loop will never be done.
I know you said this is not the case, but that's the only thing that could produce those symptoms. Try output = {}
before the loop and you'll see the problem goes away. Or you could assert(output ~= subWords)
or print(output, subWords)
to see that they're the same, but I don't know how Roblox handles those functions (it may not have an output console and/or it may trap errors).
Smells like subWords and Output is the same table. And when traversing tables, never add values to tables being traversed, as stated in the Lua Reference Manual:
[snip]
The behavior of next is undefined if, during the traversal, you assign any value to a non-existent field in the table. You may however modify existing fields. In particular, you may clear existing fields.
[snip]
And pairs()
uses next internally.
精彩评论