lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


2012/12/14 steve donovan <steve.j.donovan@gmail.com>:
> On Fri, Dec 14, 2012 at 8:19 AM, Dirk Laurie <dirk.laurie@gmail.com> wrote:
>> The documentation is much longer than the code!
>
> You know us - show us the code!  The useful difference here is (1) n
> tables (2) arrays or maps
>

Exercise for the reader with Knuth rating 12 (out of 50) I thought …
but anyway here it is (this version is a little more liberal than the
description I posted: all non-tables are treated as empty tables,
not only null):

local function merge(...)
   local s, arg = {}, {...}
   local n = select('#',...)
   for j=1,n do if type(arg[j])=='table' then
      for k,v in pairs(arg[j]) do s[k]=v end
   end end
   return s
end

Exercise for the reader (that's the 12 on the Knuth
scale, otherwise it would have been more like 6):
should the outer loop rather be done with ipairs?