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

Tables question

Started by lifewcody, 03 February 2015 - 08:16 AM
lifewcody #1
Posted 03 February 2015 - 09:16 AM
I have a table as follows:


{

  user1 = {pass = "pass", key = " ",},

}

I then have code that gets the table and compares it to the pass part of it:


if users[user1][pass] == "pass" then

-- do stuff

end

but it seems that does not work, any ideas? thanks
Bomb Bloke #2
Posted 03 February 2015 - 09:31 AM
In your second code block, user1 and pass are variables - if they aren't set to anything, then you can't index into the users table with them.

Did you mean?:

users["user1"]["pass"]

Or perhaps?:

users.user1.pass
lifewcody #3
Posted 03 February 2015 - 11:04 PM
In your second code block, user1 and pass are variables - if they aren't set to anything, then you can't index into the users table with them.

Did you mean?:

users["user1"]["pass"]

Or perhaps?:

users.user1.pass

Does not work: I am getting an error on line 29:

http://pastebin.com/zXu3dy6Q

MMLogin:29: attemp to index ? (a nil value)
KingofGamesYami #4
Posted 03 February 2015 - 11:11 PM
So you are saying your table looks like this?


{
  pData = {
    [1] = "something",
  },
}

Because that is what you are accessing on that line.
lifewcody #5
Posted 03 February 2015 - 11:23 PM
So you are saying your table looks like this?


{
  pData = {
	[1] = "something",
  },
}

Because that is what you are accessing on that line.

I have a table as follows:



{
  cody = {
    "pass",
  },
}

The computer get's sent a table that looks like this:


local a = "cody"
local b = "pass
{
  a,
  b,
}

The computer then needs to see if that username exist, if not send false back, if it does exist check to see if the passwords match, if they do, generate a key and send the key back, if the passwords do not match send false back.
Bomb Bloke #6
Posted 04 February 2015 - 12:26 AM
Let's say your file content is this:

{
  cody = {
    "pass",
  },
}

And you've read that into "users" with this:

local file = fs.open("/database/users", "r")
local dat = file.readAll()
users = textutils.unserialize(dat)
file.close()

"users" now has a key called "cody", which is another table with a numeric index at 1, which is set to "pass".

It would be a bit simpler to build the table like this:

{
  cody = "pass",
}

In which case "users" would end up with a "cody" key which was set to "pass".

Let's say you then receive a table over rednet, and stored that against, say, "incoming". If that table was built like so:

{
  "cody",
  "pass",
}

… then "incoming" would have two numeric indexes, the first being set to "cody", the second being set to "pass".

Now to check if that username and password combo existed in users, we'd merely need to do:

if incoming[1] and incoming[2] == users[incoming[1]] then

"if the first entry in the incoming table is non-nil, and the second entry is equal to the value of the key in users with the name equal to incoming's first entry, then…"
lifewcody #7
Posted 04 February 2015 - 12:33 AM
Let's say your file content is this:

{
  cody = {
	"pass",
  },
}

And you've read that into "users" with this:

local file = fs.open("/database/users", "r")
local dat = file.readAll()
users = textutils.unserialize(dat)
file.close()

"users" now has a key called "cody", which is another table with a numeric index at 1, which is set to "pass".

It would be a bit simpler to build the table like this:

{
  cody = "pass",
}

In which case "users" would end up with a "cody" key which was set to "pass".

Let's say you then receive a table over rednet, and stored that against, say, "incoming". If that table was built like so:

{
  "cody",
  "pass",
}

… then "incoming" would have two numeric indexes, the first being set to "cody", the second being set to "pass".

Now to check if that username and password combo existed in users, we'd merely need to do:

if incoming[1] and incoming[2] == users[incoming[1]] then

"if the first entry in the incoming table is non-nil, and the second entry is equal to the value of the key in users with the name equal to incoming's first entry, then…"

Everytime I create a database, that is usually how I create it, but for this I create a randomly generated key that the client can use instead of sending the username and password every time. So how would I go about in storing the key with the username?