Guest User

tab2file

a guest
Jun 30th, 2013
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.98 KB | None | 0 0
  1. tab2file = function(tab,path) -- using: tab2file(t, "file") where t is a table with values, "file" is file name
  2.   local f = fs.open(path,"w")
  3.   for k,v in pairs(tab) do
  4.     f.writeLine(k.." = "..v)
  5.   end
  6.   f.close()
  7. end
  8.  
  9. file2tab = function(path) -- using: t = file2tab("file") where t is a table to store all values, "file" is file name
  10.   tab = {}
  11.   local f = fs.open(path,"r")
  12.   local s, k, v
  13.   while true do
  14.     s = f.readLine()
  15.     if s == nil then break end
  16.     _,_,k,v = string.find(s,"^%s*([_%w]+)%s*=%s*([_%w]+)")
  17.     -- ^ is a "start of the line" character
  18.     -- (if there are any other characters like # in target string, it won't work. Use it for comments.
  19.     -- %s* is any amount (or zero) of space characters
  20.     -- [_%w]+ is any amount or alphanumeric characters and/or "_"
  21.     -- () represent a capture of this
  22.     if k ~= nil then -- if it isn't a blank string, or comment, or something else
  23.       tab[k] = v
  24.     end
  25.   end
  26.   f.close()
  27.   return tab
  28. end
Advertisement
Add Comment
Please, Sign In to add comment