Lua Module Shelve

lua-users home
wiki

Shelve

description

This module performs transparent encoding of 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 shelve.open() function in order to create or open a new shelf file, like this:

  data = shelve.open("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(data.aString)
  print("items: " .. data.aTable)

  for k,v in data.aTable do
    print(k,v)
  end

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
    print(k)
  end

or

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

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

author

AdrianPerez

Downloads

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.


FindPage · RecentChanges · preferences
edit · history
Last edited April 24, 2003 11:06 am GMT (diff)