Created
September 4, 2015 03:20
-
-
Save adamloving/0f4b67a1ae9912c00df8 to your computer and use it in GitHub Desktop.
Ruby example code for finding the intersection of two CSV files full of contacts (exported from Google contacts).
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/ruby | |
# Usage: compare-contacts.rb [file1] [file2] | |
require 'csv' | |
def find_email_column_indexes(row) | |
[ | |
row.index('E-mail Address'), | |
row.index('E-mail 2 Address'), | |
row.index('E-mail 3 Address'), | |
row.index('E-mail 1 - Value'), | |
row.index('E-mail 2 - Value'), | |
row.index('E-mail 3 - Value'), | |
row.index('E-mail 4 - Value') | |
].compact | |
end | |
def read_contacts(file_name) | |
i = 0 | |
emails = [] | |
email_indexes = nil | |
CSV.foreach(file_name) do |row| | |
if i == 0 | |
email_indexes = find_email_column_indexes(row) | |
i += 1 | |
next | |
end | |
email_indexes.each do |j| | |
email = row[j] | |
# h[email] = (h[email] || 0) + 1 if email | |
emails.push(email) if email and emails.index(email) == nil | |
end | |
i += 1 | |
end | |
emails | |
end | |
first_file_name = ARGV[0] | |
contacts1 = read_contacts first_file_name | |
puts first_file_name, contacts1.size | |
second_file_name = ARGV[1] | |
contacts2 = read_contacts second_file_name | |
puts second_file_name, contacts2.size | |
intersection = contacts1 & contacts2 | |
puts intersection |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment