Hi Im trying to take a list of usernames from a text file then add them
to an array and check the array for a login verification process. Does
anyone know how this can be done? Ive searched google and found ways of
opening a file but and reading it but not actually adding it to an
array. Or am I going about this the completely wrong way?
Posted via http://www.ruby-forum.com/.
Well, File.read read the file and give you its contents stored in a
string.
File.readlines does the same except that it returns an array where each
entry
contains a line of the file. After that, it’s only a matter of
extracting the
information you need from the string(s).
Awesome thanks guys. Yeah my file is stored like:
password1
password2
password3
So I need to split it using the \n?
so the my_array adds all the information into an array then I can run a
function like If password == my_array blah blah…
then delete it afterwards?
So If I have a variable with a password in such as pswd = example.
And in the text file there is 50 different user passwords. How can I
make a function that picks out the single word from the line of 50?
So If I have a variable with a password in such as pswd = example.
And in the text file there is 50 different user passwords. How can I
make a function that picks out the single word from the line of 50?
passwords = File.read(“passwords.txt”).split(“\n”)
pswd = ‘example’
if i = passwords.find_index(pswd)
Just to take this a bit further and give you a practical demonstration,
although you might want to look into encrypting the passwords as well
for safer storage…
Assuming the input is an already-validated tab-delimited file with
“username\tpassword” as the format:
If this is a username & password system then you’ll want to pair them
up, in which case I’d recommend storing them alongside each other and
using a Hash to check a given user’s password. In the current example
you’re giving, it looks as though someone could put in any user’s
password rather than their own and it would report back as valid.
For example:
users = %w{user1 user2}
passwords = %w{pass1 pass2} #User1 enters a password but it’s user2’s not his own
user = ‘user1’
pass = ‘pass2’
passwords.include? pass
Returns true even though it’s the wrong password for this user