如何将数据和表插入到现有的表中?

问题描述

在将新的行和表插入到已经存在的行和表中时遇到麻烦。

让我们将其称为源 SourceFile.lua 及其简化的内容:

SourceFile = {};

SourceFile.list = {
    BrandName1 = {
        number = 10,products = {
            "Product1",3,"Product2",4,"Product3",7,},BrandName2 = {
        number = 5,10,6,-- and so on
}

我想做这样的事情:

require 'SourceFile'

local myData = {
  BrandName2 = { -- add new products for the existing brand
    products = {
      "Product4",2,MyBrandName1 = { -- add new brand
    number = 12,products = {
      "MyProduct1",21,"MyProduct2",95,-- and so on
}

table.insert(SourceFile.list,myData)

但是我的代码中有什么问题,我得到以下结果(以inspect打印):

{
  list = { {
      BrandName2 = {
        products = { "Product4",2 }
      },MyBrandName1 = {
        number = 12,products = { "MyProduct1",95 }
      }
    },BrandName1 = {
      number = 10,products = { "Product1",7 }
    },BrandName2 = {
      number = 5,6 }
    }
  }
}

我在做什么错了?

我是lua的新手,很确定这很明显,但对我而言不是。请帮帮我。


添加

在回答了这些问题之后,我还找到了一种方法来逐个插入新的品牌名称:

SourceFile.list.MyBrandName1 = {
    number = 12,}

这不能完全回答我的问题,但是对lua新手来说可能有用。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)