Lua - merge tables?

前端 未结 8 1529
忘掉有多难
忘掉有多难 2020-11-28 06:45

I need to merge two tables, with the contents of the second overwriting contents in the first if a given item is in both. I looked but the standard libraries don\'t seem to

8条回答
  •  旧时难觅i
    2020-11-28 07:14

    Doug Currie's answer is the simplest for most cases. If you need more robust merging of tables, consider using the merge() method from the Penlight library.

    require 'pl'
    pretty.dump(tablex.merge({a=1,b=2}, {c=3,d=4}, true))
    
    -- {
    --   a = 1,
    --   d = 4,
    --   c = 3,
    --   b = 2
    -- }
    

提交回复
热议问题