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.