在lua中按索引值比较两个索引表

Jos*_*osh 5 lua compare lua-table

我试图比较两个相等长度的表和一个函数,因为我不知道有任何其他方法这样做.但是,使用以下功能,它无法注册,我也不知道为什么.我希望有人可以提供对此问题的深入了解,或者有更好的方法来比较这两个表.

这些表格使用以下代码填充:

str = "parameters determined by program (all digits)"
tableone = {}
for word in str:gmatch("%d") do table.insert(tableone,word) end
Run Code Online (Sandbox Code Playgroud)

两个表都是相同的,当然除了各个表名.表格正确填充,并在打印时正确显示.为了这个问题,这里有两个表:

tableone = {}
tabletwo = {}
for i=1,4 do table.insert(tableone, i) end
for i=1,4 do table.insert(tabletwo, i) end
Run Code Online (Sandbox Code Playgroud)

显然,这两个表将彼此相等.我写的比较索引表的函数如下:

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
Run Code Online (Sandbox Code Playgroud)

我试过了

print(comparetables(tableone,tabletwo))
Run Code Online (Sandbox Code Playgroud)

看它是否打印"真实"但没有运气.对我而言,它似乎应该没有问题.但事实并非如此.我错过了什么?我已经尝试过像某个人可能已经编写的table.compare函数一样的东西,但找不到这样的运气.谢谢你的任何建议!

附加信息:

我正在比较表格的原因是为了一个主人型游戏.这意味着在比较表时必须遵循以下三条规则.我创建的功能是让我开始,以为我可以从那里工作.

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

例如,使用值{1,3,3,4}的值表和{4,4,3,1}的猜测,它将返回Pcount为2(一个4和1)和一个Ccount为1 (三位在第三位).我认为最困难的部分之一就是要进行比较,以确认猜测中的第二个4不应该增加Pcount.

Mic*_*son 5

您的代码应该有一个轻微的变体:

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
Run Code Online (Sandbox Code Playgroud)

但是我使用更像这样的东西:它检查参数的类型,它们的元表以及一些其他情况.

-- This is not clever enough to find matching table keys
-- i.e. this will return false
--   recursive_compare( { [{}]:1 }, { [{}]:1 } )
-- but this is unusual enough for me not to care ;)
-- It can also get stuck in infinite loops if you use it on 
-- an evil table like this:
--     t = {}
--     t[1] = t

function recursive_compare(t1,t2)
  -- Use usual comparison first.
  if t1==t2 then return true end
  -- We only support non-default behavior for tables
  if (type(t1)~="table") then return false end
  -- They better have the same metatables
  local mt1 = getmetatable(t1)
  local mt2 = getmetatable(t2)
  if( not recursive_compare(mt1,mt2) ) then return false end

  -- Check each key-value pair
  -- We have to do this both ways in case we miss some.
  -- TODO: Could probably be smarter and not check those we've 
  -- already checked though!
  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
Run Code Online (Sandbox Code Playgroud)

这是一个使用它的例子:

print( recursive_compare( {1,2,3,{1,2,1}}, {1,2,3,{1,2,1}} ) ) -- prints true
print( recursive_compare( {1,2,3,{1,2,1}}, {2,2,3,{1,2,3}} ) ) -- prints false
Run Code Online (Sandbox Code Playgroud)


Mic*_*son 3

如果您在面向对象的意义上比较对象性比表性更强的对象,那么我会考虑以 lua OO 方式实现功能。

像这样的事情应该可以解决问题:

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

function GameState.new(a,b,c,d)
-- TODO: put argument checks here...
  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)
  -- Check it's actually a GameState, and all its bits match
  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
Run Code Online (Sandbox Code Playgroud)

然后你会像这样使用它:

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 )
Run Code Online (Sandbox Code Playgroud)