Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

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