lua-users home
lua-l archive

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



Try something like this, given your filenames are in array MyFileNames:

lua_newtable(L);
for (int i = 0; i < NumFileNames; i++)
{
    lua_pushnumber(L,i);
    lua_pushstring(L,MyFileNames[i]);
    lua_settable(L,-3);
}
return 1;

that should make a numbered table array of strings and return it back to LUA. It should be simple enough to grasp, we create a new table on top of the stack. Then push key -> value pairs onto the stack one at a time, which lua_settable() pops off and inserts into the table below the 2 keys for every iteration.

Jason

From: "Andy Bushnell" <andy@carbongraphics.com>
Reply-To: Lua list <lua@bazar2.conectiva.com.br>
To: <lua@bazar2.conectiva.com.br>
Subject: C Function returning a table to LUA?
Date: Mon, 29 Sep 2003 01:18:13 -0600

Hi,

Please excuse my ignorance (i'm a Lua newbie).

I have a 'C' function (getFileListing) that has been registered with Lua. It
compiles a list of files in the current working directory. Everything works
fine until I try *returning* this info to Lua. How can I do it?

My Lua script (right now) makes the call...

getFileListing()

Nothing happens but printf's in the getFileListing function show that its
being called & doing the right thing. So now I have an array of strings (the file names) in the 'C' side but I want to pass this info back to the script. I think I need to use a "Table" to pass this info to Lua, but tables are new
to me and I cant see an example which does what I want (which, as a newbie,
im looking for!).

I'd like my script (I think) to look something like ...

t = getFileListing() -- where t is the returned table

-- check for table size

-- iterate through table getting the file listing names

(im not sure of the exact table iteration syntax - but I can work that out
later).

I hope this isnt too simple a question - but any help would greatly
appreciated.

Regards,

Andy




_________________________________________________________________
Add MSN 8 Internet Software to your existing Internet access and enjoy patented spam protection and more. Sign up now! http://join.msn.com/?page=dept/byoa