Created
February 15, 2010 14:50
-
-
Save rklemme/304692 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'set' | |
# information about a single line | |
LineInfo = Struct.new :file_name, :lineno, :line | |
# comparison data | |
FileData = Struct.new :file_a, :file_b | |
# read all words from first file and store in a Set | |
words = File.foreach(ARGV.shift).map {|l| l.chomp!}.to_set.freeze | |
# storage for maintaining matches | |
matches = Hash.new {|h,words| h[words] = FileData.new [], []} | |
# read first file | |
f = ARGV.shift | |
File.foreach f do |line| | |
w = words & line.scan(/\w+/) | |
matches[w].file_a << LineInfo.new(f, $., line) unless w.empty? | |
end | |
# read second file | |
f = ARGV.shift | |
File.foreach f do |line| | |
w = words & line.scan(/\w+/) | |
matches[w].file_b << LineInfo.new(f, $., line) unless w.empty? | |
end | |
# output matches | |
matches.each do |words, file_data| | |
unless file_data.file_a.empty? || file_data.file_b.empty? | |
p words, file_data | |
puts | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment