4

I have a table of values that can in principle be of any length:

Points =
{
    "Point #1",
    "Point #5",
    "Point #7",
    "Point #10",
    "Point #5",
    "Point #11",
    "Point #5",
}

I want to pass them as arguments to a function.

addPath(<sPathName>, <sPoint>, <sPoint>, ...)

Now, normally you could use the "call" function. But in the software I am using this function is unavailable and not in scope.

How do I get around this problem in Lua 4?

[edit]

Here are the functions I can use.

1 Answer 1

6

In newer versions of Lua, you'd use unpack, as in addPath(sPathName,unpack(Points)), but Lua 4.0 does not have unpack.

If you can add C code, unpack from Lua 5.0 works fine in 4.0:

static int luaB_unpack (lua_State *L) {
  int n, i;
  luaL_checktype(L, 1, LUA_TTABLE);
  n = lua_getn(L, 1);
  luaL_checkstack(L, n, "table too big to unpack");
  for (i=1; i<=n; i++)  /* push arg[1...n] */
    lua_rawgeti(L, 1, i);
  return n;
}

Add this to lbaselib.c and this to base_funcs:

  {"unpack", luaB_unpack},

If you cannot add C code, then you're out of luck and are probably reduced to this hack:

function unpack(t)
  return t[1],t[2],t[3],t[4],t[5],t[6],t[7],t[8],t[9],t[10]
end

Extend the return expression as needed but you may only go as far as 200 or so. Let's hope that addPath ignores or stops at the first nil.

You can also try this one, which stops at the first nil but has no explicit limits (there are recursion limits and it'll only handle up to 250 table entries):

function unpack(t,i)
        i = i or 1
        if t[i]~=nil then
                return t[i],unpack(t,i+1)
        end
end
Sign up to request clarification or add additional context in comments.

1 Comment

How do I then pass them to the function?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.