当前位置 : 主页 > 网络编程 > lua >

如何从表成员中引用lua表成员?

来源:互联网 收集:自由互联 发布时间:2021-06-23
我在lua有一张桌子: enUS = { LOCALE_STHOUSANDS = ",", --Thousands separator e.g. comma patNumber = "%d+["..LOCALE_STHOUSANDS.."%d]*", --regex to find a number ["PreScanPatterns"] = { ["^("..patNumber..") Armor$"] = "ARMOR", }} 所以你
我在lua有一张桌子:

enUS = {
   LOCALE_STHOUSANDS = ",",  --Thousands separator e.g. comma

   patNumber = "%d+["..LOCALE_STHOUSANDS.."%d]*", --regex to find a number

   ["PreScanPatterns"] = {
      ["^("..patNumber..") Armor$"] = "ARMOR",
   }
}

所以你看到这个表中有一整串自引用:

> LOCAL_STHOUSANDS

> patNumber

> [“^(”.. patNumber ..“)护甲$”]

如何在lua表中执行自引用?

我不想做的是必须硬替换价值观;有数百个参考:

enUS = {
   LOCALE_STHOUSANDS = ",",  --Thousands separator e.g. comma

   patNumber = "%d+[,%d]*", --regex to find a number

   ["PreScanPatterns"] = {
      ["^(%d+[,%d]*) Armor$"] = "ARMOR",
   }
}

How can i perform self-referencing in an lua table?

你没有.

Lua不是C.在构造表之前,不存在任何表条目.因为表本身尚不存在.因此,您不能在表构造函数中有一个条目引用不存在的表中的另一个条目.

如果你想减少重复输入,那么你应该使用局部变量和do / end块:

do
  local temp_thousands_separator = ","
  local temp_number_pattern = "%d+["..LOCALE_STHOUSANDS.."%d]*"

  enUS = {
   LOCALE_STHOUSANDS = temp_thousands_separator,  --Thousands separator e.g. comma

   patNumber = "%d+["..temp_thousands_separator.."%d]*", --regex to find a number

   ["PreScanPatterns"] = {
      ["^("..temp_number_pattern..") Armor$"] = "ARMOR",
   }
  }
end

do / end块在那里,以便临时变量不存在于表创建代码之外.

或者,您可以分阶段进行施工:

enUS = {}
  enUS.LOCALE_STHOUSANDS = ",",  --Thousands separator e.g. comma

  enUS.patNumber = "%d+["..enUS.LOCALE_STHOUSANDS.."%d]*", --regex to find a number

  enUS["PreScanPatterns"] = {
      ["^("..enUS.patNumber..") Armor$"] = "ARMOR",
   }
网友评论