Created
August 30, 2010 19:28
-
-
Save jamesmacaulay/557905 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
#!/usr/bin/env ruby | |
# call with filenames as arguments | |
# outputs lines which appear in all files | |
# (stripping leading and trailing whitespace, in no predictable order) | |
# $ cat > foo | |
# apple | |
# orange | |
# pear | |
# $ cat > bar | |
# pear | |
# orange | |
# pineapple | |
# $ cat > baz | |
# pineapple | |
# apple | |
# orange | |
# $ ./intersection.rb foo bar baz | |
# orange | |
require 'set' | |
lines = {} | |
ARGF.each_line do |line| | |
(lines[ARGF.filename] ||= Set.new) << line.strip | |
end | |
sets = lines.values | |
intersection = sets.inject(sets.pop) {|intersection,set| intersection & set} | |
intersection.each do |line| | |
puts line | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment