Skip to content

Instantly share code, notes, and snippets.

@rklemme
Created February 15, 2010 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rklemme/304692 to your computer and use it in GitHub Desktop.
Save rklemme/304692 to your computer and use it in GitHub Desktop.
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