<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users1883</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/1883" rel="self"/>
  <title>vire7</title>
  <updated>Fri Jan 15 17:46:09 -0800 2010</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code1146</id>
    <published>2010-01-15T17:46:09-08:00</published>
    <updated>2010-05-07T16:01:22-07:00</updated>
    <title>[Ruby] Making email addresses out of common names</title>
    <content type="html">&lt;p&gt;I have a text file with just first last name. Some middle initials. I needed to read that list and then create usernames out of it, finding the duplicates. Can you help me clean this up or teach me something new?&lt;/p&gt;

&lt;pre&gt;original_file = File.open('content1.txt', 'r')
new_file = File.open('master1.txt', 'w')

master_list = []
counter = 0
counter_written = 0

original_file.each_line do |line|
  counter += 1
  name = line.split(' ')

  # Handle multi-worded surnames ('Von Bluth')
  if name.length &amp;gt; 2
    name[1] = name[1] + name[2]
    name.pop
  end

  # Handle hyphenated names (Bluth-Funke)
  if name[1] =~ /-/
    name[1] = name[1].split('-').pop
  end
 
  username = &amp;quot;#{name[0][0].chr}#{name[1]}&amp;quot;.downcase
  master_list &amp;lt;&amp;lt; username
  counter_written += 1 if new_file.puts(&amp;quot;#{username}@somedomain.com&amp;quot;).nil?
end

original_file.close
new_file.close

duplicates = master_list.inject({}){|h,v| h[v]=h[v].to_i+1; h}.reject{|k,v| v==1}.keys

master_list.sort.each {|username| puts username + '@somedomain.com'}
puts &amp;quot;\n\n&amp;quot;
puts &amp;quot;- - - - - - - - - - - - - - - - - - - -&amp;quot;
puts &amp;quot;Processed: #{counter}&amp;quot;
puts &amp;quot;Written: #{counter_written}&amp;quot;
puts &amp;quot;Master List: #{master_list.size}&amp;quot;
puts &amp;quot;- - - - - - - - - - - - - - - - - - - -&amp;quot;
puts &amp;quot;Duplicates: #{duplicates.size}&amp;quot;
puts &amp;quot;#{duplicates.join(', ')}&amp;quot;
puts &amp;quot;\n\n&amp;quot;&lt;/pre&gt;</content>
    <author>
      <name>vire7</name>
      <email>vineire77@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1146-making-email-addresses-out-of-common-names" rel="alternate"/>
  </entry>
</feed>

