Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops LUA使用字符串从表中获取值_Loops_Lua_Lua Table - Fatal编程技术网

Loops LUA使用字符串从表中获取值

Loops LUA使用字符串从表中获取值,loops,lua,lua-table,Loops,Lua,Lua Table,如何使用字符串从表中编译值? i、 e 例如,如果我请求1ABC3,我将如何使其输出1 3 非常感谢您的回复。您可以访问lua数组中的值,如下所示: 表名[索引名或编号] 以您的例子: NumberDef = { [1] = 1, [2] = 2, [3] = 3 } TextDef = { ["a"] = 1, ["b"] = 2, ["c"] = 3 } print(NumberDef[2])--Will print 2 print(TextDef["c"])--will print

如何使用字符串从表中编译值? i、 e

例如,如果我请求1ABC3,我将如何使其输出1 3


非常感谢您的回复。

您可以访问lua数组中的值,如下所示:

表名[索引名或编号]

以您的例子:

NumberDef = {
[1] = 1,
[2] = 2,
[3] = 3
}

TextDef = { 
["a"] = 1,
["b"] = 2,
["c"] = 3
}

print(NumberDef[2])--Will print 2
print(TextDef["c"])--will print 3
如果希望访问Lua数组的所有值,可以循环访问所有值,类似于c中的foreach:

for i,v in next, TextDef do
print(i, v) 
end
--Output:
--c    3
--a    1
--b    2
因此,要回答您的请求,您可以这样请求这些值:

print(NumberDef[1], TextDef["a"], TextDef["b"], TextDef["c"], NumberDef[3])--Will print   1 1 2 3 3
还有一点,如果您对连接lua字符串感兴趣,可以这样完成:

string1=string2。。弦3

例如:

local StringValue1 = "I"
local StringValue2 = "Love"

local StringValue3 = StringValue1 .. " " .. StringValue2 .. " Memes!"
print(StringValue3) -- Will print "I Love Memes!"
更新 我快速编写了一个示例代码,您可以使用它来处理您正在寻找的内容。这将检查输入的字符串,并检查两个表中是否存在您请求的值。如果它这样做了,它将把它添加到一个字符串值上,并在最后打印最终产品

local StringInput = "1abc3" -- Your request to find  
local CombineString = "" --To combine the request

NumberDef = {
[1] = 1,
[2] = 2,
[3] = 3
}

TextDef = { 
["a"] = 1,
["b"] = 2,
["c"] = 3
}

for i=1, #StringInput do --Foreach character in the string inputted do this
    local CurrentCharacter = StringInput:sub(i,i); --get the current character from the loop position
    local Num = tonumber(CurrentCharacter)--If possible convert to number

    if TextDef[CurrentCharacter] then--if it exists in text def array then combine it
        CombineString = CombineString .. TextDef[CurrentCharacter] 
    end
    if NumberDef[Num] then --if it exists in number def array then combine it
        CombineString = CombineString .. NumberDef[Num] 
    end
end

print("Combined: ", CombineString) --print the final product.
试试这个:

s="1ABC3z9"

t=s:gsub(".",function (x)
    local y=tonumber(x)
    if y~=nil then
        y=NumberDef[y]
    else
        y=TextDef[x:lower()]
    end
    return (y or x).." "
end)

print(t)

如果将两个表合并为一个表,这可能会简化。

嗨,真正的问题,尽管我的措辞可能不正确,但有没有一种方法可以遍历这些表,让它们打印1ABC3的值,而不必在发布时手动获取值?您可以将数字表和文本表合并到一个数组中,因为lua与混合值数组兼容,因此可以生成循环代码一次遍历字符串并索引每个值,将它们合并并返回请求的值。因此,我将分解字符串并遍历组合表中的每个值?感谢您的帮助,这似乎解决了这个难题:-
s="1ABC3z9"

t=s:gsub(".",function (x)
    local y=tonumber(x)
    if y~=nil then
        y=NumberDef[y]
    else
        y=TextDef[x:lower()]
    end
    return (y or x).." "
end)

print(t)