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

Separating values in a table

Started by Chaz, 09 September 2016 - 02:02 AM
Chaz #1
Posted 09 September 2016 - 04:02 AM
Hey there! So I'm trying to set up a little system where each user is set up with a password, and I'm trying to read out the values just to make sure everything's working out okay. So far I have things set up like so:


usrtable = {}
usrtable[1]={usr="User1", pass="pass1"},
usrtable[2]={usr="User2", pass="pass2"},
usrtable[3]={usr="User3", pass="pass3"},
--etc etc
file=fs.open("user", "w")
file.write(textutils.serialise(usrtable))
file.close()

This results in a serialised table that looks something like


{
  {
    usr="User1",
	pass="pass1",
  },
  {
	usr="User2",
	pass="pass2",
  },
  {
	usr="User3",
	pass="pass3",
  },
}

I've then proceeded to unserialise this file in another program, and have written the following code:


for _, v in pairs(usertable) do
  for _, u in pairs (v) do
	print(u)
  end
end

This returns:


User1
pass1
User2
pass2
User3
pass3

How do I break this down into showing the usernames and passwords separately? For instance:
"User: "..user.." Pass: "..pass
Edited on 09 September 2016 - 02:03 AM
valithor #2
Posted 09 September 2016 - 04:07 AM
You could do something like this. Essentially it loops through the top level table (As you were already doing), but instead of looping through each individual nested table, it will just index them and get the information you are wanting.


for _, v in ipairs(usertable) do
  print("User: "..v.usr.." Pass: "..v.pass)
end
Edited on 09 September 2016 - 02:19 AM
Dog #3
Posted 09 September 2016 - 04:07 AM
Try this…

for i = 1, #usertable do
  print("User: " .. usertable[i].usr .. " Pass: " .. usertable[i].pass)
end

:ph34r:/> 'd
Edited on 09 September 2016 - 02:08 AM
Chaz #4
Posted 09 September 2016 - 04:42 AM
That solution was a lot simpler than I was expecting! Thank you for the help, both of you! While I've been at ComputerCraft for a while I've been scratching my head with regards to tables, I'll get there in the end though!

EDIT: I do have another question, actually, apologies for the sudden edit.

Now that I know how to separate values, I'm trying to figure out how to use the value from a matched entry. Long story short I'm using the Mag-Stripe Reader from Immibis' Peripherals, and I'm programming a card-writer script. So what I think I want to do is:

1: Ask the user for a text input. (Easily enough done)
2: Check the database to see if the value matches up with any names in the user-list database.
Lyqyd helped me out with this a while back, the code I use is:


local matchfound=false

for _, v in pairs(usrtable) do
  local usr=v.usr
  if input==usr then
	matchfound=true
	break
  end
end	

3: If a match is found, write the password to the mag-stripe card, and the name to the card's label. (Technically I could also do this with floppy disks, but Immibis' mag-stripe cards look cool!)

That last part is what's tripping me up - All I have it set to do right now is check to see if it matches, I'm not sure how to then use that to write data from the matching entry somewhere else.

EDIT: Nevermind, I figured it out! It was another simple change, haha. I modified the code above so it now reads:

local matchfound=false

for _, v in pairs(usrtable) do
  local usr=v.usr
  if input==usr then
	matchfound=true
    musr=v.usr
    mpass=v.pass
	break
  end
end	

This means that when the input matches, it not only returns the "Match Found = True" value, but also makes variables for the User and Pass of the instance that matched!

Long story short:


Rubber-duck debugging - As soon as I explain my problem I figure out a solution. :D/>
Edited on 09 September 2016 - 03:19 AM