lua中通过索引值比较两个索引表
我试图用一个函数比较两个长度相等的表,因为我不知道还有什么其他方法可以做到这一点。但是,使用以下功能时,它无法注册,我不知道为什么。我希望有人能够深入了解这个问题,或者有更好的方法来比较两个表。
这些表使用以下代码填充:
str = "parameters determined by program (all digits)"
tableone = {}
for word in str:gmatch("%d") do table.insert(tableone,word) end
这两个表都是相同的,当然,各个表名称除外。这些表格已正确填充,并且在打印时正确显示。为了解决这个问题,这里有两个表:
tableone = {}
tabletwo = {}
for i=1,4 do table.insert(tableone, i) end
for i=1,4 do table.insert(tabletwo, i) end
显然,这两个表将彼此相等。我编写的用于比较索引表的函数如下:
function comparetables(t1, t2)
matchct = 0
for i=1,#t1 do
if t1[i] == t2[i] then
matchct = matchct + 1
end
if matchct == #t1 then
return true
end
end
我尝试
print(comparetables(tableone,tabletwo))
查看它是否会打印“true”,但没有成功。对我来说,它似乎应该毫无问题地工作。但事实并非如此。我缺少什么?我尝试过搜索类似 table.compare 函数之类的东西,可能有人已经编写过,但没有找到这样的运气。感谢您的任何建议!
附加信息:
我比较表格的原因是针对主主类型的游戏。这意味着在比较表时必须应用以下三个规则。我创建的功能只是为了让我开始,我认为我可以从那里开始工作。
- 比较表时,如果数字匹配,则 Ccount 加 1。
- 比较表时,如果值存在于不同的索引位置,则 Pcount 加 1
例如,对于值 {1, 3, 3, 4} 的表如果猜测为 {4, 4, 3, 1},它将返回 Pcount 为 2(第 4 个和第 1 个)和 Ccount 为 1(第三个位置上的三个)。我认为最困难的部分之一是通过比较来认识到猜测中的第二个 4 根本不应该增加 Pcount。
I'm attempting to compare two tables of equal length with a function, since I don't know of any other way to do so. However, with the following function, it fails to register, and I've no clue why. I'm hoping someone can provide insight to this problem or has a better way of comparing the two tables.
The tables are being populated with the following code:
str = "parameters determined by program (all digits)"
tableone = {}
for word in str:gmatch("%d") do table.insert(tableone,word) end
It's identical for both tables, except, of course, the individual table names. The tables are being populated properly, and display properly when I print them. Here are two tables for the sake of this question:
tableone = {}
tabletwo = {}
for i=1,4 do table.insert(tableone, i) end
for i=1,4 do table.insert(tabletwo, i) end
Obviously, these two tables are going to be equal to each other. The function I wrote to compare the index tables is as follows:
function comparetables(t1, t2)
matchct = 0
for i=1,#t1 do
if t1[i] == t2[i] then
matchct = matchct + 1
end
if matchct == #t1 then
return true
end
end
I tried doing
print(comparetables(tableone,tabletwo))
to see if it'll print "true" but no luck. To me, it seems like it should work without a problem. Yet it doesn't. What am I missing? I've tried searching for something like a table.compare function that someone may have already written, but no such luck in finding one. Thanks for any suggestions!
Additional information:
The reason I'm comparing tables is for a mastermaind-type game. That means the following three rules must apply when comparing tables. The function I created was to just get me started, thinking I could work from there.
- When comparing the tables, if the numbers match, Ccount increases by 1.
- When comparing tables, if the value exists in a different index position, increment Pcount by 1
For example, with a table of values {1, 3, 3, 4} and a guess of {4, 4, 3, 1}, it would return Pcount of 2 (the one 4 and the 1) and a Ccount of 1 (the three in the third position). I think one of the hardest parts is going to be getting the comparison to recognize that the second 4 in the guess should not increment the Pcount at all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
应该有效的代码的一个细微变体是:
但是我使用更像这样的东西:它检查参数的类型、它们的元表以及其他一些情况。
这是一个使用中的示例:
A slight variant on your code that should work is:
However I use something more like this: It checks the types of the arguments, their metatables, and a few other cases.
Here's an example of it in use:
如果您在面向对象的意义上比较对象性比表性更强的对象,那么我会考虑以 lua OO 方式实现功能。
像这样的东西应该可以解决问题:
然后你可以像这样使用它:
If you're comparing objects that are more objecty than tabley in an Object oriented sense, then I'd look at implementing the functions in the lua OO way.
Something like this should do the trick:
Then you'd use it like this: