Jump to content




Read an array from an external file


  • You cannot reply to this topic
3 replies to this topic

#1 Siramnot

  • New Members
  • 2 posts

Posted 24 May 2014 - 03: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 :)

#2 CCJJSax

  • Members
  • 262 posts

Posted 25 May 2014 - 12: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


#3 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 25 May 2014 - 01:00 AM

View PostCCJJSax, on 25 May 2014 - 12:50 AM, said:

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
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


#4 Siramnot

  • New Members
  • 2 posts

Posted 25 May 2014 - 03:48 AM

View PostCCJJSax, on 25 May 2014 - 12:50 AM, said:

-snip-

View Posttheoriginalbit, on 25 May 2014 - 01:00 AM, said:

-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 by Siramnot, 26 May 2014 - 06:24 PM.






2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users