# Searching in arrays

**URL:** <https://www.ruby-forum.com/t/searching-in-arrays/183445>\
**Category:** Ruby\
**Created:** [February 14, 2010, 12:20pm UTC](https://www.ruby-forum.com/t/searching-in-arrays/183445 "2010-02-14T12:20:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tszabi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e5b9ba/32.png) [@tszabi](https://www.ruby-forum.com/u/tszabi)\
**Post date:** [February 14, 2010, 12:20pm UTC](https://www.ruby-forum.com/t/searching-in-arrays/183445/1 "2010-02-14T12:20:49Z")

</div>

Dears, I have two .txt files. I read both into arrays and I would like  
to match the lines containing the same words. The words should come from  
a separate text (input.txt) file. This is what I have done:

# Open the file - get a file handle

test1 = File.new “OpenSystemHO.txt”  
test2 = File.new “OpenSystemDR.txt”

myArray= Array.new  
myArray=[] # start with an empty array  
test1.each\_line {|line|  
myArray.push line  
}

myArray2= Array.new  
myArray2=[] # start with an empty array  
test2.each\_line {|line|  
myArray2.push line  
}

while interest = gets

myArray.each {|e| print e,"\n" if e.include?(interest)}  
myArray2.each {|e2| print “\*”,e2,"\n" if e2.include?(interest)}  
end

Problem 1: if I have only 1 word in the text file to search it works  
fine but if I have two, searching only the second (last) one.

Problem 2: how can I write one file from one array one from the other  
and not grouping the result of array1 and array2.

thank you in adavance: Szabolcs

---

<div class="post-metadata">

**Author:** ![tszabi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e5b9ba/32.png) [@tszabi](https://www.ruby-forum.com/u/tszabi)\
**Post date:** [February 14, 2010, 6:13pm UTC](https://www.ruby-forum.com/t/searching-in-arrays/183445/2 "2010-02-14T18:13:10Z")

</div>

On Sun, Feb 14, 2010 at 4:20 AM, Szabolcs T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> test1.each\_line {|line|  
> myArray.push line  
> }

### CODE BEGINS

# You can initialize each array in a single line:

array1 = File.open(“OpenSystemHO.txt”, ‘r’).map{|x| x.chomp}  
array2 = File.open(“OpenSystemDR.txt”, ‘r’).map{|x| x.chomp}

# Your code includes the end-of-line character(s) in each

# array entry. Note the “chomp” above to trim off end-of-line

# characters. You could use your existing code and add a

# chomp:

# myArray.push line.chomp

# 

# Your ‘interest = gets’ line also includes end-of-line

# character(s). May I suggest adding a chomp! too?

# 

# WARNING: This will match ALL lines of BOTH files

# if interest is blank/empty:

counter = 0 # Counter for unique file naming (see below)  
while interest = gets  
interest.chomp! # Remove end-of-line character(s)

# You can create an array containing ONLY matching

# lines using the Array#select() method:

matches1 = array1.select{|e| e.include?(interest)}  
matches2 = array2.select{|e| e.include?(interest)}

# Print the matches:

puts “Lines (#{matches1.length}) from OpenSystemHO.txt that match  
‘#{interest}’:”  
puts matches1 # One match per line  
puts “Lines (#{matches2.length}) from OpenSystemDR.txt that match  
‘#{interest}’:”  
puts matches2.map{|m| '_’ + m} # One match per line prepended with  
"_"

# Save matches to files:

counter += 1 # Counter for unique file naming

puts “Saving array1 matches to file:  
OpenSystemHO.match-#{counter}.txt”  
File.new(“OpenSystemHO.match-#{counter}.txt”, ‘w’).puts(matches1)

puts “Saving array2 matches to file:  
OpenSystemHO.match-#{counter}.txt”  
File.new(“OpenSystemDR.match-#{counter}.txt”, ‘w’).puts(matches2)  
end

### CODE ENDS

> Problem 1: if I have only 1 word in the text file to search it works  
> fine but if I have two, searching only the second (last) one.

I’m not sure exactly what you are saying. I suspect it has to do with  
your code matching only the line ends of each file (due to your code  
including line terminator characters).

> Problem 2: how can I write one file from one array one from the other  
> and not grouping the result of array1 and array2.

The above sample code also shows you how you could write the matches  
to an output file or files (if desired).

I hope that’s helpful.

Aaron out.

---

<div class="post-metadata">

**Author:** ![tszabi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e5b9ba/32.png) [@tszabi](https://www.ruby-forum.com/u/tszabi)\
**Post date:** [February 14, 2010, 8:04pm UTC](https://www.ruby-forum.com/t/searching-in-arrays/183445/3 "2010-02-14T20:04:21Z")

</div>

Aaron, this is BRILLIANT. Exactly does what I expected.  
Thank you very much!  
Szabolcs

---

<div class="post-metadata">

**Author:** ![tszabi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e5b9ba/32.png) [@tszabi](https://www.ruby-forum.com/u/tszabi)\
**Post date:** [February 15, 2010, 3:55pm UTC](https://www.ruby-forum.com/t/searching-in-arrays/183445/4 "2010-02-15T15:55:59Z")

</div>

2010/2/14 Szabolcs T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Aaron, this is BRILLIANT. Exactly does what I expected.

Interesting. I would have thought you wanted to match all lines that  
contain the exact same subset of words from your interest file. In  
other words: read a line from a file, find all words that are in the  
interest file and check whether the other file has a line containing  
the exact same set of words.

That could be done like this: [word-match.rb · GitHub](http://gist.github.com/304692)

Kind regards

robert
