Lua tables store non-array elements in an arbitrary order. And unpacking is worthless without some control over the order of the generated elements. So it's not possible to usefully unpack the non-array portion of a table.
Now, you can still do it; you just won't have any kind of control over the order. So it's not clear what the point would be, but the code would look like this:
function unpack_unordered_recursive(tbl, key)
local new_key, value = next(tbl, key)
if new_key == nil then return end
return value, unpack_unordered_recursive(tbl, new_key)
end
function unpack_unordered(tbl)
local key, value = next(tbl)
if key == nil then return end
return value, unpack_unordered_recursive(tbl, key)
end
However, if you have an array table that contains the list of keys to extract and the order to extract them in, then you can write an unpack function that uses such a table:
function unpack_indices(tbl, indices, curr_ix)
if curr_ix == nil then
return unpack_indices(tbl, indices, 1)
end
if curr_ix > #indices then
return
end
--Recursive call
return tbl[indices[curr_ix]], unpack_indices(tbl, indices, curr_ix + 1)
end
print("t:", unpack_indices(t, {"b", "a"}))