Lua Module Shelve

lua-users home

shelve is a Lua module that transparently encodes Lua values (numbers, strings, booleans and tables) into files. Each stored element is accessible through a key, so you can treat a shelf file like you would treat a regular Lua table.

Quick Tips

Use the function in order to create or open a new shelf file, like this:

data ="my_data_file")

The data variable acts lika a Lua table, so you can store keyed values and read them using the known Lua syntax:

-- store some values
data.aNumber = 10
data.aString = "this is a string"
data.aTable  = 
     "these", "are", "some", "strings", "contained",
     "in", "a", "nested", "table",
     nested = true,
     items  = 9

-- retrieve values, and print them
print("items: " .. data.aTable)

for k,v in data.aTable do

If you want to know which keys are present in i shelf file, just do a call, as if the shelf was a function, and it will return a table with the same keys as the shelf file:

-- retrieve keys and print them
keys = data()
for key in keys do


-- retrieve keys and print them
for key in data() do

To manually close a shelf file, simply assign nil to it, and the Lua GarbageCollection will close it for you. Just stick this line to the example above to close the file:

data = nil




At this moment it's only available as source code. Latest version is 0.33. Version prior to 0.33 were buggy, so updating to version 0.33 or above is highly recommended.


You will need Lua 5.0 beta or above, and either Gdbm or Ndbm. Gdbm is recommended. Current version was tested with MacOS X 10.2 (Jaguar) and Linux.

RecentChanges · preferences
edit · history
Last edited October 31, 2009 10:12 pm GMT (diff)