lua-users home
lua-l archive

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

>I'm writing routines for saving/restoring Lua table to/from file

See also lua/test/save.lua.

>How can I find out the name of object from the object, i.e
>print(?) -- must print 'foo'

If foo is a global variable, then you can search the table of globals:

 local obj=get_object_somehow()
 name=foreach(globals(), function (i,v) if v==%obj then return i end end)

This will find *one* name for the object, not necessarily the one you expect.
For instance, if you do "xyz=print", then the code above may return "print"
or "xyz". If you want all names, then you can collect them in a table.

Anyway, objects in Lua are really anonymous, much like files in Unix.
They can have several global names or none at all, for they can be fields
of a table. When no other object points to them, they are deleted.