Using Ruby to Find Common Lines in X files
Question
Right now I have 3 files, but I'd like to do this in a way that I can add more later. Each file is a list of IDs, like this.
174535945 174538045 160515924 81712260 25241494
I'd like the output to be the items that appear in list 1 and list 2, list 2 and list 3, and those that occur in list 1 and list 2 and list 3.
Would the most ruby way to be create a hash with a key for each list, and then get all keys and test against all hashes, or is there a nice gem that would help with this?
Thanks,
Solution
Use sets:
require 'set'
list_1 = open(filename_1).read.split.to_set
list_2 = open(filename_2).read.split.to_set
list_3 = open(filename_3).read.split.to_set
puts list_1 & list_2
puts list_2 & list_3
puts list_1 & list_2 & list_3
OTHER TIPS
I think your suggestion of using a hash is the way to go:
require 'set'
class Test
def initialize()
@lists = {174=>[1,2,3], 111=>[2,3,4], 160=>[2,4,5]}
end
def common_members(*keys)
lists_as_sets = {}
@lists.each_pair {|key,list| lists_as_sets[key] = list.to_set}
common = lists_as_sets[keys.shift] # Set to key[0] and nil that element of keys
keys.each {|k| common = common & lists_as_sets[k]} # Intersect the remaining keys, if any
common.to_a
end
end
t = Test.new
p t.common_members(174,111) # => [2,3]
p t.common_members(111,160) # => [2,4]
p t.common_members(174,111,160) # =>[2]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow