2012-02-22 59 views
0

我試圖使用Ruby的IMAP庫來獲取所有電子郵件發件人(「from」)的列表,然後按字母順序對其進行排序,然後計算每個郵件的數量人。按字母順序排序在ruby中的imap.fetch

我越來越掛在第1步 - 按字母順序排序。這是我有的代碼,它返回所有「from」值的列表,但它們絕對不是按字母順序排列的。

完成紅寶石新手在這裏 - 少於1周,所以請溫柔。

mail_count = imap.search(["SINCE", @this_week.strftime("%d-%b-%Y")]).each do |message_id| 
    envelope = imap.fetch(message_id, "ENVELOPE")[0].attr["ENVELOPE"] 
from_array = envelope.from[0].name.to_a 
sorted_from = from_array.sort 
puts "#{sorted_from}" 
end 

回答

0

也許這:

results = [] 
mail_count = imap.search(["SINCE", @this_week.strftime("%d-%b-%Y")]).each do |message_id| 
    envelope = imap.fetch(message_id, "ENVELOPE")[0].attr["ENVELOPE"] 
    from_array = envelope.from[0].name.to_a 
    results << from_array 
end 
results.sort.each do |el| 
    puts "#{el}" 
end 
+0

這個工作!現在我可以繼續研究如何計算重複項,然後生成一個有序列表e1:重複次數 – krapdagn 2012-02-22 05:33:30