在lua中通过索引值比较两个索引表

我正在尝试使用一个函数来比较两个等长的表,因为我不知道其他的方法。然而,使用下面的函数时,它无法被识别,我不知道为什么。我希望有人能够解决这个问题,或者有更好的方法来比较这两个表。

这些表是通过以下代码填充的:

“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函数,但是没有这样的好运。感谢任何建议!

其他信息:

我比较表的原因是为了mastermind类型的游戏。这意味着比较表时必须应用以下三个规则。我创建的函数只是让我开始思考,以便我可以从那里工作。

  1. 比较表时,如果数字匹配,则Ccount增加1。
  2. 比较表时,如果值存在于不同的索引位置,则Pcount增加1。

例如,具有{1,3,3,4}值的表和{4,4,3,1}的猜测将返回Pcount为2(一个4和1)和Ccount为1(第三个位置的三)。我认为最难的部分之一将是使比较认识到猜测中的第二个4完全不应该增加Pcount。

原文链接 https://stackoverflow.com/questions/8722620

点赞
stackoverflow用户221955
stackoverflow用户221955

一个稍微改进过的代码,应该能够工作:

function comparetables(t1, t2)
  if #t1 ~= #t2 then return false end
  for i=1,#t1 do
    if t1[i] ~= t2[i] then return false end
  end
  return true
end

不过我更喜欢使用下面这个代码:它会检查参数的类型、它们的元表以及其他一些情况。

-- 这并不聪明到可以找到匹配的表键
-- 例如,这会返回 false
--   recursive_compare( { [{}]:1 }, { [{}]:1 } )
-- 但这个情况很少,我也不会在意 ;)
-- 如果你在一个邪恶的表上使用它,它也有可能卡在无限循环中:
--     t = {}
--     t[1] = t

function recursive_compare(t1,t2)
  -- 首先使用通常的比较。
  if t1==t2 then return true end
  -- 我们只支持对表进行非默认行为的比较
  if (type(t1)~="table") then return false end
  -- 它们必须有相同的元表
  local mt1 = getmetatable(t1)
  local mt2 = getmetatable(t2)
  if( not recursive_compare(mt1,mt2) ) then return false end

  -- 检查每个键值对
  -- 我们需要以两种方式进行检查,以防我们错过一些情况。
  -- TODO: 可以更聪明,不需要检查我们已经检查过的键值对!
  for k1,v1 in pairs(t1) do
    local v2 = t2[k1]
    if( not recursive_compare(v1,v2) ) then return false end
  end
  for k2,v2 in pairs(t2) do
    local v1 = t1[k2]
    if( not recursive_compare(v1,v2) ) then return false end
  end

  return true
end

下面是一个使用示例:

print( recursive_compare( {1,2,3,{1,2,1}}, {1,2,3,{1,2,1}} ) ) -- 输出 true
print( recursive_compare( {1,2,3,{1,2,1}}, {2,2,3,{1,2,3}} ) ) -- 输出 false
2012-01-04 06:26:21
stackoverflow用户221955
stackoverflow用户221955

如果你正在比较的对象在面向对象的感觉上比在表格上更像对象,那么我会看一下是否可以用 Lua 的面向对象方式来实现这些函数。

可以尝试像这样实现:

GameState = {}
GameState.mt = {}
GameState.mt.fns = {}
GameState.mt.__index = GameState.mt.fns

function GameState.new(a,b,c,d)
-- TODO: 在此处放置参数检查...
  local retval = {}
  retval[1] = a
  retval[2] = b
  retval[3] = c
  retval[4] = d
  setmetatable(retval, GameState.mt)
  return retval
end

function GameState.mt.fns.print( self )
  print(" GameState: ", self[1], self[2], self[3], self[4] )
end

function GameState.mt.__tostring( self )
  return "GameState: "..self[1].." "..self[2].." "..self[3].." "..self[4]
end

function GameState.mt.__eq(self, other)
  -- 检查是否为 GameState,以及其所有位是否匹配
  return getmetatable(other)==GameState.mt and
    (self[1] == other[1]) and
    (self[2] == other[2]) and
    (self[3] == other[3]) and
    (self[4] == other[4])
end

然后按照以下方式使用它:

state1 = GameState.new(1,2,3,4)
state2 = GameState.new(1,2,3,4)

print("State 1 is:")
state1:print()

print("State 2 is:")
print(state2)

print( "state1 == state2 : ", state1 == state2 )

print( "Changing state 2")
state2[1]=2

print( "state1 == state2 : ", state1 == state2 )
2012-01-04 06:54:25