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

**URL:** <https://www.ruby-forum.com/t/how-to-take-information-from-a-text-file-and-add-them-to-an-array/225512>\
**Category:** Ruby\
**Created:** [January 12, 2013, 2:14pm UTC](https://www.ruby-forum.com/t/how-to-take-information-from-a-text-file-and-add-them-to-an-array/225512 "2013-01-12T14:14:18Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [January 12, 2013, 2:14pm UTC](https://www.ruby-forum.com/t/how-to-take-information-from-a-text-file-and-add-them-to-an-array/225512/1 "2013-01-12T14:14:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![therealadam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8797f3/32.png) [@therealadam](https://www.ruby-forum.com/u/therealadam)\
**Post date:** [January 12, 2013, 2:26pm UTC](https://www.ruby-forum.com/t/how-to-take-information-from-a-text-file-and-add-them-to-an-array/225512/2 "2013-01-12T14:26:17Z")

</div>

On Saturday 12 January 2013 Adam K. wrote

> Posted via [http://www.ruby-forum.com/](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

---

<div class="post-metadata">

**Author:** ![therealadam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8797f3/32.png) [@therealadam](https://www.ruby-forum.com/u/therealadam)\
**Post date:** [January 12, 2013, 2:26pm UTC](https://www.ruby-forum.com/t/how-to-take-information-from-a-text-file-and-add-them-to-an-array/225512/3 "2013-01-12T14:26:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![therealadam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8797f3/32.png) [@therealadam](https://www.ruby-forum.com/u/therealadam)\
**Post date:** [January 12, 2013, 2:32pm UTC](https://www.ruby-forum.com/t/how-to-take-information-from-a-text-file-and-add-them-to-an-array/225512/4 "2013-01-12T14:32:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![therealadam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8797f3/32.png) [@therealadam](https://www.ruby-forum.com/u/therealadam)\
**Post date:** [January 12, 2013, 3:00pm UTC](https://www.ruby-forum.com/t/how-to-take-information-from-a-text-file-and-add-them-to-an-array/225512/5 "2013-01-12T15:00:14Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![therealadam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8797f3/32.png) [@therealadam](https://www.ruby-forum.com/u/therealadam)\
**Post date:** [January 12, 2013, 2:54pm UTC](https://www.ruby-forum.com/t/how-to-take-information-from-a-text-file-and-add-them-to-an-array/225512/6 "2013-01-12T14:54:22Z")

</div>

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/)

---

<div class="post-metadata">

**Author:** ![therealadam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8797f3/32.png) [@therealadam](https://www.ruby-forum.com/u/therealadam)\
**Post date:** [January 12, 2013, 5:01pm UTC](https://www.ruby-forum.com/t/how-to-take-information-from-a-text-file-and-add-them-to-an-array/225512/7 "2013-01-12T17:01:06Z")

</div>

On Sat, Jan 12, 2013 at 8:00 AM, Adam K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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

---

<div class="post-metadata">

**Author:** ![therealadam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8797f3/32.png) [@therealadam](https://www.ruby-forum.com/u/therealadam)\
**Post date:** [January 12, 2013, 3:01pm UTC](https://www.ruby-forum.com/t/how-to-take-information-from-a-text-file-and-add-them-to-an-array/225512/8 "2013-01-12T15:01:22Z")

</div>

Am 12.01.2013 14:32, schrieb Adam K.:

> Sorry if that sounds confusing 🙂
> 
> Adam K

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

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

---

<div class="post-metadata">

**Author:** ![therealadam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8797f3/32.png) [@therealadam](https://www.ruby-forum.com/u/therealadam)\
**Post date:** [January 12, 2013, 8:08pm UTC](https://www.ruby-forum.com/t/how-to-take-information-from-a-text-file-and-add-them-to-an-array/225512/9 "2013-01-12T20:08:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![therealadam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8797f3/32.png) [@therealadam](https://www.ruby-forum.com/u/therealadam)\
**Post date:** [January 12, 2013, 10:21pm UTC](https://www.ruby-forum.com/t/how-to-take-information-from-a-text-file-and-add-them-to-an-array/225512/10 "2013-01-12T22:21:50Z")

</div>

Am 12.01.2013 20:08, schrieb Joel P.:

> temparray.each { |ar| users\_passwords[ar[0]] = ar[1] }  
> or: users\_passwords = Hash[temparray]

---

<div class="post-metadata">

**Author:** ![therealadam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8797f3/32.png) [@therealadam](https://www.ruby-forum.com/u/therealadam)\
**Post date:** [January 12, 2013, 5:17pm UTC](https://www.ruby-forum.com/t/how-to-take-information-from-a-text-file-and-add-them-to-an-array/225512/11 "2013-01-12T17:17:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![therealadam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8797f3/32.png) [@therealadam](https://www.ruby-forum.com/u/therealadam)\
**Post date:** [January 14, 2013, 12:49am UTC](https://www.ruby-forum.com/t/how-to-take-information-from-a-text-file-and-add-them-to-an-array/225512/12 "2013-01-14T00:49:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![therealadam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8797f3/32.png) [@therealadam](https://www.ruby-forum.com/u/therealadam)\
**Post date:** [January 13, 2013, 12:48am UTC](https://www.ruby-forum.com/t/how-to-take-information-from-a-text-file-and-add-them-to-an-array/225512/13 "2013-01-13T00:48:49Z")

</div>

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.
