在lua中将表格转换为小写

Monster_List = {'Hunter','creature','demon'}

Monster_List = Monster_List:lower()

那么关于

Attacks = {}

Attacks[1] = {'CreaTurE','MonstEr'}
Attacks[2] = {'FrOG', 'TurtLE'}

如果这看起来太笨了,我要如何将表中所有内容设置为小写?

编辑:对于第二个问题,我是这样做的,不确定是否正确

for i=1,#Attacks do
    for k,v in pairs(Attacks[i]) do
    Attacks[i][k] = v:lower()
    end
end
点赞
用户1208078
用户1208078

迭代表并更新其值。

lst = {'BIRD', 'Frog', 'cat', 'mOUSe'}
for k,v in pairs(lst) do
    lst[k] = v:lower()
end

table.foreach(lst, print)

输出结果为:

1   bird
2   frog
3   cat
4   mouse

要处理嵌套的表,递归函数会让事情变得轻松。像这样?

lst = {
    {"Frog", "CAT"},
    {"asdf", "mOUSe"}
}

function recursiveAction(tbl, action)
    for k,v in pairs(tbl) do
        if ('table' == type(v)) then
            recursiveAction(v, action)
        else
            tbl[k] = action(v)
        end
    end
end

recursiveAction(lst, function(i) return i:lower() end)
-- just a dirty way of printing the values for this specific lst
table.foreach(lst, function(i,v) table.foreach(v, print) end)

输出结果为:

1   frog
2   cat
1   asdf
2   mouse
2014-05-01 19:47:12