lua-users home
lua-l archive

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


> Any advices?

You could try the following (not fully tested and using 5.0 syntax):
function iff(cond, a, b)
  if cond then
    return a
  else
    return b
  end
end

function newtable(...)
  local result = {}

  for i = 1, table.getn(arg) do
    local v = arg[i]

    if v ~= nil then
      table.insert(result, v)
    end
  end

  return result
end

-- Usage
A, B, C, D, E, F = 1, 2, 3, 4, 5, 6
Cond1 = true
Cond2 = false

t = newtable(A, B, iff(Cond1, C, D), iff(Cond2, E, nil), F)

for i, v in ipairs(t) do
  print(i, v)
end
print()

Cond1 = false
Cond2 = true

t = newtable(A, B, iff(Cond1, C, D), iff(Cond2, E, nil), F)

for i, v in ipairs(t) do
  print(i, v)
end
print()

Marius Gheorghe