Searching in arrays

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

On Sun, Feb 14, 2010 at 4:20 AM, Szabolcs T. [email protected]
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.

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

2010/2/14 Szabolcs T. [email protected]:

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

Kind regards

robert