使用For循环获取Lua表条目

时间:2019-04-29 15:28:51

标签: for-loop lua

我用Lua制作了一张桌子和游戏脚本。其概念如下: 首先,用户猜测从表中获得一个显示随机词(第一个索引),如果用户回答正确,则显示下一个随机词(下一表项),依此类推,直到显示所有表项。

如何在Lua中使用for..do使其循环?

我当前的代码:

private void Register( HttpConfiguration configuration ) 
{
    configuration.MessageHandlers.Add( new EnsureNoAuthenticationHeaderHandler() );
}

1 个答案:

答案 0 :(得分:0)

已解决:下面的示例

 data = {
        {id = 'ronald', say = 'reagan'},
        {id = 'tom', say = 'hank'},
        {id = 'angelina', say = 'jolie'}
        }

 function tablelength(T)
   local count = 0
   for _ in pairs(T) do count = count + 1 end
   return count
 end

 count = tablelength(data)
 i = 1
 edt = UDF1.CEEdit1  -- edt is the name of textbox

 function CEEdit1KeyPress(sender, key)
  answer = data[i]
  v_word = answer.id..' '..answer.say
  tipo = string.lower(UDF1.CEEdit1.Text)

  if tipo == "" and isKeyPressed(VK_RETURN)== true then return nil end

  if tipo ~= v_word then
   if isKeyPressed(VK_RETURN) == true then
   showMessage('Try again..')
   edt.Text = ''
  end
  elseif tipo == v_word then
   if isKeyPressed(VK_RETURN) == true then
   showMessage('OK. Then answer is : '..v_word)
   edt.Text = ''
   i = i+1
    if i > count then showMessage('All words have displayed') end
  end
 end
 return key
end

UDF1.show()  --Show Form