2088 posts
Location
South Africa
Posted 10 October 2012 - 05:57 PM
I found this login system that someone had uploaded to pastebin and the person didn't leave their name so I'll ask here.
for i=1, #users do
if userEntered == users[i] then
userNum = i
break
end
end
After entering the user name this code appears, could someone explain to me how this works because I'm completely clueless of how it finds the username within the file.
users is declared as a table.
userEntered reads the username.
EDIT: Another question - I just realized now that the input it reads is case sensitive to what is saved inside the file. i.e. if i register as Sidekick_ and when i try login with sidekick_ it fails and doesn't recognize it. How would I make it not case sensitive or a better way: how would i make it change all upper case letters into lower case letters when registering and logging in?
818 posts
Posted 10 October 2012 - 06:09 PM
I think this was from my password program, and it basically loops for as many variables there are in the table users
then it checks if the username entered is equal to variable number 1 in the table users. if that is, it will change the variable userNum to the current loop number. And then it will break the loop. if it's not equal, then it will loop again, and next loop it will take the second variable in users, because we are in the second loop. and if it's correct here it will set the current loop number to userNum and break. to make it not case sensitive, do
userEntered = string.lower(userEntered)
in the beginning. This is useful for multi acc password programs.
2088 posts
Location
South Africa
Posted 10 October 2012 - 06:47 PM
I think this was from my password program, and it basically loops for as many variables there are in the table users
then it checks if the username entered is equal to variable number 1 in the table users. if that is, it will change the variable userNum to the current loop number. And then it will break the loop. if it's not equal, then it will loop again, and next loop it will take the second variable in users, because we are in the second loop. and if it's correct here it will set the current loop number to userNum and break. to make it not case sensitive, do
userEntered = string.lower(userEntered)
in the beginning. This is useful for multi acc password programs.
Ahh yes, I recognize your name ;)/>/> Nice code btw (if it is yours :P/>/>). And thanks for explaining. I've been fooling around with it for quite a while now… And thanks for showing me how to make strings lowercase :)/>/>
If this is your code, could you explain one more thing to me?
function readUsersAndPasswords()
fs.makeDir("passwordFolder")
regUser = fs.open("passwordFolder/users","a")
regPassword = fs.open("passwordFolder/passwords","a")
regUser.close()
regPassword.close()
users = {}
passwords = {}
userFile = fs.open("passwordFolder/users","r")
passwordFile = fs.open("passwordFolder/passwords","r")
lineRead = userFile.readLine()
repeat
table.insert(users,lineRead)
lineRead = userFile.readLine()
until lineRead == nil
lineRead = passwordFile.readLine()
repeat
table.insert(passwords,lineRead)
lineRead = passwordFile.readLine()
until lineRead == nil
passwordFile.close()
userFile.close()
end
The part where it repeats, does it enter the line read each time into the users / passwords table? And when it reads nil it ends?
818 posts
Posted 10 October 2012 - 07:59 PM
function readUsersAndPasswords() -- declares the function
fs.makeDir("passwordFolder") -- makes a new directory where we will put the saves
regUser = fs.open("passwordFolder/users","a") -- opens a new file in appeal mode, so that if the file does not exist it will create the file. if you open a non existing file in read mode it will just be equal to nil and wont create a file
regPassword = fs.open("passwordFolder/passwords","a") -- same as above but with another file
regUser.close() -- closes the file opened in appeal
regPassword.close() -- closes the other file opened in appeal
users = {} -- pretty much empties the table users
passwords = {} -- empties the table passwords
userFile = fs.open("passwordFolder/users","r") -- opens the user file in read mode
passwordFile = fs.open("passwordFolder/passwords","r") -- opens the password file in read mode
lineRead = userFile.readLine() -- reads the first line in the user file
repeat -- Starts a repeat loop
table.insert(users,lineRead) -- inserts the line read into a new variable in the users table. Since we emptied it before it will be the first variable in this table too
lineRead = userFile.readLine() -- reads the next line for insertion.
until lineRead == nil -- checks if the next line is equal too nil, if so, it will end the repeat loop
lineRead = passwordFile.readLine( ) -- This whole thing from here to the while is just the same thing but with the password file and the passwords table
repeat
table.insert(passwords,lineRead)
lineRead = passwordFile.readLine()
until lineRead == nil
passwordFile.close() -- closes the password file
userFile.close() -- closes the users file
end -- ends the funciton declaration
2088 posts
Location
South Africa
Posted 10 October 2012 - 08:28 PM
Thanks :P/>/>
136 posts
Posted 10 October 2012 - 09:15 PM
If you create the table in reverse such as:
users = {["Jeff"] = 1, ["Mary"] = 2,}
You can increase the efficiency of the code in a couple ways.
userNum = users[userEntered]
if not userNum then return end --users not on file will return nil
818 posts
Posted 10 October 2012 - 09:22 PM
I don't think you understand my whole code :P/>/>
136 posts
Posted 10 October 2012 - 09:23 PM
Glancing through the forums at work :3 Just looked at OP
Your reply tells me that you have indeed thought of efficiency whilst coding. <3 Carry on.