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

···

--
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 Saturday 12 January 2013 Adam Kennedy wrote

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

--
Posted via http://www.ruby-forum.com/\.

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

···

--
Posted via http://www.ruby-forum.com/.

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

···

--
Posted via http://www.ruby-forum.com/.

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?

···

--
Posted via http://www.ruby-forum.com/.

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.

···

--
Posted via http://www.ruby-forum.com/.

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

···

--
Posted via http://www.ruby-forum.com/.

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.

···

--
Posted via http://www.ruby-forum.com/.

or:

names = File.read('names.txt').split(/\n/)

···

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

--
<https://github.com/stomar/&gt;

passwords = ['password', '12345', 'abcde']

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

···

Am 12.01.2013 14:32, schrieb Adam Kennedy:

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

--
<https://github.com/stomar/&gt;

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 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?

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] }

or: users_passwords = Hash[temparray]

···

Am 12.01.2013 20:08, schrieb Joel Pearson:

p users_passwords

--
<https://github.com/stomar/&gt;

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.

--
Posted via http://www.ruby-forum.com/\.