This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Siramnot's profile picture

Read an array from an external file

Started by Siramnot, 24 May 2014 - 02:04 PM
Siramnot #1
Posted 24 May 2014 - 04:04 PM
So, I'm trying to make a log-in system for a program, and I would like to be able to have a separate file, containing the users' usernames and passwords, in a lua-array style, kind of like this:

This file would be called "users", and it would reside under "/misc"

users = { }

user.firstuser = "foo"
user.seconduser = "bar"
user.thirduser = "baz"


As you could guess, the password for the "firstuser" account is "foo".
I would like to be able to load this array into a separate file, to be able to achieve this:

This file would be my startup file

users = <whatever should go here in order to load the "users" array from "/misc/users">

io.write("Username: ")
local username = io.read():lower()
io.write("Password: ")
local password = io.read("*")

if users[username] == nil then
  print("Could not find said account")
  return false

elseif users[username] == password then
  print("Welcome, " .. username. . "!")
  return true

else
  print("Your password is incorrect...")
  return false
end


Any help is really appreciated :)/>
CCJJSax #2
Posted 25 May 2014 - 01:50 AM
I used to have a hard time understanding how to work with files, then I found these function by someone here on the forums. I think his name was mystict.


local function readLines(sPath)
  local file = fs.open(sPath, "r")
  if file then
	    local tLines = {}
	    local sLine = file.readLine()
	    while sLine do
		  table.insert(tLines, sLine)
		  sLine = file.readLine()
	    end
	    file.close()
	    return tLines
  end
  return nil
end
local function writeLines(sPath, tLines)
  local file = fs.open(sPath, "w")
  if file then
	    for _, sLine in ipairs(tLines) do
		  file.writeLine(sLine)
	    end
	    file.close()
  end
end
theoriginalbit #3
Posted 25 May 2014 - 02:00 AM
I used to have a hard time understanding how to work with files, then I found these function by someone here on the forums. I think his name was mystict.
Spoiler

local function readLines(sPath)
  local file = fs.open(sPath, "r")
  if file then
		local tLines = {}
		local sLine = file.readLine()
		while sLine do
		  table.insert(tLines, sLine)
		  sLine = file.readLine()
		end
		file.close()
		return tLines
  end
  return nil
end
local function writeLines(sPath, tLines)
  local file = fs.open(sPath, "w")
  if file then
		for _, sLine in ipairs(tLines) do
		  file.writeLine(sLine)
		end
		file.close()
  end
end
some cleanup and stuff ;)/>

local function readLines(sPath)
  local fHandle = fs.open(sPath, 'r')
  if fHandle then
    local tLines = {}
    for sLine in fHandle.readLine do
      tLines[#tLines+1] = sLine
    end
    fHandle.close()
    return tLines
  end
end

local function writeLines(sPath, tLines)
  local fHandle = fs.open(sPath, 'w')
  if fHandle then
    fHandle.writeLine(table.concat(tLines, '\n'))
    fHandle.close()
  end
end
Siramnot #4
Posted 25 May 2014 - 04:48 AM
-snip-

-snip-

Thanks for the help! I'll give it a try as soon as I get a chance, and I'll post feedback :P/>

EDIT (feedback):
So, I didn't really manage to achieve exactly what I was looking for…
I was looking for something that would take a file, and load an array from it as-is.
I implemented the code above:

-- Users.lua
users = { }
users.foo = "bar"
users.bar = "baz"
users.baz = "foo"


-- Startup.lua
function readLines(sPath)
  ...
end

users = readLines("misc/users")
for k,v in pairs(users) do
  print(k .. ":  " .. v)
  --[[
  This would print:
1: users.foo = "bar"
2: users.bar = "baz"
3: users.baz = "foo"

  What I was looking for:
foo: "bar"
bar: "baz"
baz: "foo"
  ]]--
end

Edited on 26 May 2014 - 05:24 PM