gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 23:06:08 25 4
gpt4 key购买 nike

我在卢阿有一张 table :

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..") Armor$"]

  • 如何在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",
    }

    关于lua - 如何从表成员引用lua表成员?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11856047/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com