lua-users home
lua-l archive

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


On Thursday 23 February 2006 11:50 am, Chris wrote:
> That made me think, a substring style unpack could be useful.

this seems to work:


------ C --------
  int split (lua_State *L) {
    int i = 0;
    int n = luaL_checkint (L, 1);
    int rest = lua_gettop (L) - 1 -n;
		
    lua_remove (L, 1);
    lua_newtable (L);
    while (rest-- > 0) {
      lua_pushvalue (L, n+1);
      lua_remove (L, n+1);
      lua_rawseti (L, -2, ++i);
    }
		
    return n+1;
  }


----- Lua ------

a,b,c = split (2, 5, 6, 7, 8, 9)
print (a, b, c)
    =>  5    6    table: 0x808f2f8
table.foreach (c, print)
    => 1       7
          2       8
          3       9

--------

of course, i tested it with LuaTCC  :-)

-- 
Javier

Attachment: pgpqOlTFqCA6N.pgp
Description: PGP signature