Created
August 24, 2011 17:33
-
-
Save seeflanigan/1168624 to your computer and use it in GitHub Desktop.
Select Names with Matching Last Name and First Initial from a list
This file contains hidden or 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 'csv' | |
| CSV_FILENAME = "some_names.csv" | |
| names_hash = {} | |
| matches_array = [] | |
| CSV.foreach(CSV_FILENAME) do |row| | |
| last_name,first_name = row[0],row[1] | |
| if names_hash[last_name].nil? | |
| names_hash[last_name] = [] | |
| end | |
| names_hash[last_name].push(first_name) | |
| end | |
| names_hash.map do |last_name, first_names| | |
| initials = first_names.map {|name| name[0]}.uniq | |
| initials.each do |initial| | |
| matching_names = first_names.select {|n| n[0] == initial} | |
| if matching_names.count > 1 | |
| matching_names.each do |first_name| | |
| matches_array << "#{first_name} #{last_name}" | |
| end | |
| end | |
| end | |
| end | |
| puts matches_array |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment