Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal

Archived

This topic is now archived and is closed to further replies.

SpaceBeeGaming

Table saver/loader

Recommended Posts

I wrote this simple library for saving and loading tables from files as I needed such functionality for my program. (Useful for saving program settings between reboots.)

Requirements: OpenOS

Documentation:

load(string: path)
  Returns the table stored in given path.
save(table, string: path)
  Saves the table in a file located in path
local serialization = require("serialization")

local tableToFile = {}

function tableToFile.load(location)
  --returns a table stored in a file.
  local tableFile = assert(io.open(location))
  return serialization.unserialize(tableFile:read("*all"))
end

function tableToFile.save(table, location)
  --saves a table to a file
  local tableFile = assert(io.open(location, "w"))
  tableFile:write(serialization.serialize(table))
  tableFile:close()
end

return tableToFile

Usage:

local ttf=require("tableToFile")

--Saving
local data={"a","b"}
ttf.save(data,"/tmp/dataTable.txt")

--loading
local data=ttf.load("/tmp/dataTable.txt")

If you have suggestions on how to improve it, please comment. (or make a pull request/issue in the GitHub repo.)

This can also be found from here: https://github.com/SpaceBeeGaming/OC-Script-Collection/blob/master/lib/tableToFile.lua

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.