How to take information from a text file and add them to an array

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 Saturday 12 January 2013 Adam K. 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

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

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 :slight_smile:

Adam K

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?

Am 12.01.2013 14:26, schrieb Joel P.:

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 Sat, Jan 12, 2013 at 8:00 AM, Adam K. [email protected]
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

Am 12.01.2013 14:32, schrieb Adam K.:

Sorry if that sounds confusing :slight_smile:

Adam K

passwords = [‘password’, ‘12345’, ‘abcde’]

passwords.include?(‘12345’) # => true
passwords.include?(‘hello’) # => false

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

Am 12.01.2013 20:08, schrieb Joel P.:

temparray.each { |ar| users_passwords[ar[0]] = ar[1] }
or: users_passwords = Hash[temparray]

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.

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.

unknown wrote in post #1092079:

Am 12.01.2013 20:08, schrieb Joel P.:

temparray.each { |ar| users_passwords[ar[0]] = ar[1] }
or: users_passwords = Hash[temparray]

Nice! I didn’t know about that trick.