Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Problem with logic in making a cross element for many tables.

edited June 16 Posts: 848

Here’s my current code which works, but not like intended for more than 2 tables,

table.multiply = function(...)
args = {...}
local v = {}

local function g(n, m, result) -- {a, {b, {c, ...}}} -> {a, b, c, ...}
if m == 1 then
return
else
for a, b in ipairs() do

end
end
end

if #args == 0 then -- 0 = error
return {}
elseif #args == 1 then
return args
elseif #args == 2 then
local k = {}
for a, b in ipairs(args) do
for c, d in ipairs(args) do
table.insert(k, {b, d})
end
end
return k
else
local v = args

while #v > 1 do
local a = v[#v]
local b = v[#v-1]
local c = table.multiply(a, b)
v[#v] = nil
v[#v] = c -- new #v-1
end

--v = g(v, #args)

return table.multiply(v)
end
end

I’m trying to make something like below.

table.multiply({1, 2}, {3, 4}, {5, 6}) -> [{1, 3, 5}, {1, 3, 6}, {1, 4, 5}, ... {2, 4, 6}]

But in my mistake is apparently that the table doesn’t get unpacked like needed: {{1, 3}, 5} <- WRONG