【问题标题】:Concatenating Various Tables In Lua在 Lua 中连接各种表
【发布时间】:2012-09-29 19:40:16
【问题描述】:

几天来我一直在寻找这个问题的答案,我设法以某种方式使用一个技巧来省略这个连接部分,并使用几个单独的循环将不同的值重新插入到同一个表中...... 但我的问题是

默认情况下,table.sort 使用

A = { {} , {} , {} , "" , "a", "b" , "c" , 1 , 2 , 3 , -100 , 1.1 , function() end , function() end , false , false , true }

正如我所说,我使用不同的 for 循环解决了这个问题,但是有没有办法只分析表的每个元素,然后将其分配给不同的表???比如:"Tables,Funcs,Nums,Strings,..." 然后在分析完成后将它们连接在一起以在排序版本中拥有相同的表。

我对此的低效回答是:

function Sep(val)
local NewA = {}

   for i in pairs(val) do
      if type(val[i]) == "string" then
     table.insert(NewA,val[i])
      end
   end
for i in pairs(val) do
      if type(val[i]) == "number" then
     table.insert(NewA,val[i])
      end
   end

for i in pairs(val) do
      if type(val[i]) == "function" then
     table.insert(NewA,tostring(val[i]))
      end
   end

for i in pairs(val) do
      if type(val[i]) == "table" then
     table.insert(NewA,tostring(val[i]))
      end
   end

for i in pairs(val) do
      if type(val[i]) == "boolean" then
     table.insert(NewA,tostring(val[i]))
      end
   end

for i in pairs(NewA) do
   print(NewA[i])
end
end

【问题讨论】:

    标签: sorting loops lua


    【解决方案1】:

    据我了解,您希望先按类型排序,然后按值排序: 您可以编写自己的自定义谓词并将其传递给排序

    -- there would be need of custom < function since tables cannot be compared
    -- you can overload operator < as well I suppose.
     function my_less (lhs, rhs)
        if (type (lhs) ~= "number" or type (lhs) ~= "string") then
            return tostring (lhs) < tostring (rhs) 
        else
            return lhs < rhs;
        end;
     end;
    
     -- the custom predicate I mentioned
     function sort_predicate (a,b)
        -- if the same type - compare variable, else compare types
        return (type (a) == type (b) and my_less (a, b)) or type (a) < type (b);
     end
    
     table.sort (A, sort_predicate);
    

    【讨论】:

    • 您可以将整个内容简化为 4 行。您需要做的就是检查 type(lhs) == "number" 是否返回 lhs
    • 我不明白为什么我们必须在 my_less 函数中输入两个值?它不只是函数中 Table 类型的输入,然后只是以预定义的方式将其排序出来......?
    • 这是一个比较功能 - 你比较。比较本质上涉及比较至少两个对象。
    • 我试图暗示你可以为每种类型编写任何类型的运算符
    • 我刚刚更新了问题并添加了我的问题,正如@MikeCorcoran 提到的那样,不需要额外的类型,尤其是我这样做的方式,但是您能否再解释一下代码,我是“在 Lua,Wrox 中编程”==>> 之后的新内容,我没有得到将两个或多个不同的表值连接在一起并将它们添加到包含所有这些值的单个表中的方法的答案。
    【解决方案2】:
    A = { {}, {}, {}, "", "a", "b", "c", "2", "12", 1, 2, 3, -100, 1.1, 12, 11,
      function() end, function() end, false, false, true }
    
    table.sort(A, function(a,b)
      if type(a) == type(b) and type(a) == 'number' then return a < b end
      return type(a)..tostring(a) < type(b)..tostring(b) end)
    

    会给你这个:

    {false, false, true, function() end, function() end,
     -100, 1, 1.1, 2, 3, 11, 12, "", "12", "2", "a", "b", "c", {}, {}, {}}
    

    [根据@tozka 的 cmets 更新]

    【讨论】:

    • 这不会以正确的顺序对数字进行排序A = { 1, 2, 21, 12 } 会给出这个1, 12, 2, 21
    • @tozka,更新了一个稍微复杂的版本,应该可以处理这个问题。
    • 现在你几乎遇到了相反的问题,而A = { 2, 12 } 将正常排序(因为我们正在排序数字)。 A = { "2", "12" }(请注意,现在我们正在排序字符串而不是数字)将给出 { "2", "12" },其中字符串的正确排序顺序是 { "12", "2" }
    • 对;我会争辩说这样更“自然”;)
    • @downvoters,很高兴对您不同意的内容发表评论。风格?物质?格式化?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-27
    • 2022-12-01
    相关资源
    最近更新 更多