小写lua表

时间:2014-05-01 19:42:26

标签: string lua lua-table lowercase

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

1 个答案:

答案 0 :(得分:3)

迭代表并更新值。

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