加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Lua将表格插入表格

发布时间:2020-12-14 21:45:03 所属栏目:大数据 来源:网络整理
导读:基本表,它们应该如何.但我需要按功能去做,我该怎么做? local mainMenu = { caption = "Main Window",description = "test window",buttons = { { id = 1,value = "Info" },{ id = 2,value = "Return" },{ id = 3,value = "Ok" },{ id = 4,value = "Cancel"
基本表,它们应该如何.但我需要按功能去做,我该怎么做?

local mainMenu = {
  caption = "Main Window",description = "test window",buttons = {
  { id = 1,value = "Info" },{ id = 2,value = "Return" },{ id = 3,value = "Ok" },{ id = 4,value = "Cancel" }
  },popup = true
  }

表应该基于外部参数,并为每个变量选项编写一个表 – 不是更好的方法.我为此创建了一个函数,他们应该创建基本选项,如标题或描述并弹出,并将值插入按钮表(如果启用了选项 – 添加按钮).但在这里的问题,他们不会插入到tmp表,按钮表及其下一个选项的值.

function createMenu()
    tmp = {}
    --buttons insert
   if(config.info) then
    table.insert(tmp,{buttons = {id = 1,value = "Info"}});
   elseif(config.return) then
    table.insert(tmp,{buttons = {id = 2,value = "Return"}});
   end
    --table main
   table.insert(tmp,{
    caption = "Main Window",popup = true
    })
     return tmp
   end

我怎么能修理它们?

解决方法

从查看你的createMenu函数,出现了两个明显的问题:

>每次createMenu都为全局tmp分配一个新表
调用.
>使用return关键字作为配置中的键.

如果在createMenu函数之外的代码中的其他地方使用tmp,则可能会出现问题.显而易见的解决方法是将其更改为:

local tmp = {}

对于第二个问题,如果你真的想要,你可以使用lua关键字作为表键,但是你将无法使用. dot语法来访问它,因为Lua将以错误的方式解析它.相反,你需要改变:

config.return

config["return"].

编辑:读完注释并检查示例表后,看起来只有按钮表才能通过数字索引访问.在这种情况下,您只想在按钮上使用table.insert.如果要创建具有关联键的表,则必须执行以下操作:

function createMenu()
  local tmp = 
  {
    --table main
    caption = "Main Window",popup = true,--button table
    buttons = {}
  }
  --buttons insert
  if config.info then
    table.insert(tmp.buttons,{id = 1,value = "Info"});
  elseif config['return']  then
    table.insert(tmp.buttons,{id = 2,value = "Return"});
  end

  return tmp
end

这将生成您在问题中描述的mainMenu表.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读