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? Thanks for your help Adam K
on 2013-01-12 14:14
on 2013-01-12 14:26
On Saturday 12 January 2013 Adam Kennedy wrote
> 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).
Stefano
on 2013-01-12 14:26
Exactly what you do depends on how your input is formatted, but say you
had one name per line:
my_array = File.open("example.txt", "r").read.split("\n")
my_array.delete ''
p my_array
on 2013-01-12 14:32
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? Sorry if that sounds confusing :) Adam K
on 2013-01-12 14:54
Am 12.01.2013 14:26, schrieb Joel Pearson: > Exactly what you do depends on how your input is formatted, but say you > had one name per line: > > my_array = File.open("example.txt", "r").read.split("\n") > my_array.delete '' > p my_array > or: names = File.read('names.txt').split(/\n/)
on 2013-01-12 15:00
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?
on 2013-01-12 15:01
Am 12.01.2013 14:32, schrieb Adam Kennedy: > Sorry if that sounds confusing :) > > Adam K passwords = ['password', '12345', 'abcde'] passwords.include?('12345') # => true passwords.include?('hello') # => false
on 2013-01-12 17:01
On Sat, Jan 12, 2013 at 8:00 AM, Adam Kennedy <lists@ruby-forum.com> wrote: > 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) # do stuff with found password at index i else # not found end
on 2013-01-12 17:17
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
Whereas if you adopt an approach like this:
users_passwords = { 'user1' => 'pass1', 'user2' => 'pass2' }
user = 'user1'
pass = 'pass2'
users_passwords[user] == pass
Returns false, which is right, that isn't his password.
on 2013-01-12 20:08
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:
users_passwords = Hash.new
temparray = File.read('input.txt').split(/\n/).map! { |line|
line.split("\t") }
temparray.each { |ar| users_passwords[ar[0]] = ar[1] }
p users_passwords
on 2013-01-12 22:21
Am 12.01.2013 20:08, schrieb Joel Pearson:
> temparray.each { |ar| users_passwords[ar[0]] = ar[1] }
or: users_passwords = Hash[temparray]
on 2013-01-13 00:48
unknown wrote in post #1092079: > Am 12.01.2013 20:08, schrieb Joel Pearson: >> temparray.each { |ar| users_passwords[ar[0]] = ar[1] } > or: users_passwords = Hash[temparray] Nice! I didn't know about that trick.
on 2013-01-14 00:49
And as always with Ruby, there's an even simpler way to write it:
users_passwords = Hash[ File.foreach('input.txt').map { |line|
line.chomp.split("\t") } ]
p users_passwords
I think that foreach would handle large files better than read or
readlines too.
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.