2013-04-25 90 views
0

我正在導入iphone日誌的csv文件,並希望按日期對呼叫進行分組,並將每天的呼叫輸出到.txt文件。csv按日期散列,分組和保存爲.txt

我這樣導入:

src = <<csv 
"name","Date","Duration","Call Type" 
"Henry Jones ","2013-04-04 19:30:19","0:00:49","Outgoing" 
"Alistair Morgan ","2013-04-05 13:14:07","0:00:03","Outgoing" 
"Quentin Blah","2013-04-05 13:19:44","0:04:28","Missed" 
"Quentin Blah","2013-04-05 13:25:19","0:09:45","Incoming" 
"Henry Jones ","2013-04-05 14:35:29","0:00:24","Incoming" 
"Henry Jones ","2013-04-05 15:54:53","0:00:00","Missed" 
"Henry Jones ","2013-04-06 16:21:20","0:00:47","Outgoing" 
"Bill and Ben Saunders ","2013-04-06 18:33:00","0:00:32","Outgoing" 
"Phil Philly","2013-04-08 08:56:06","0:01:18","Outgoing" 
"Barry Smith ","2013-04-08 13:51:03","0:00:48","Incoming" 
"John Goole ","2013-04-08 18:24:57","0:06:28","Incoming" 
"John Goole ","2013-04-08 18:32:07","0:00:35","Outgoing" 
"Barry Barry","2013-04-08 18:33:29","0:01:56","Outgoing" 
"Phil Michelson","2013-04-09 09:32:14","0:20:30","Outgoing" 
"Nick Badson ","2013-04-09 12:00:28","0:00:00","Incoming" 
"Henry Jones ","2013-04-09 15:09:02","0:00:00","Outgoing" 
csv 

require 'csv' 
csv_data = CSV.parse (src) 

headers = csv_data.shift.map {|i| i.to_s } 
string_data = csv_data.map {|row| row.map {|cell| cell.to_s } } 
array_of_hashes = string_data.map {|row| Hash[*headers.zip(row).flatten] } 

p array_of_hashes 

我現在努力知道如何輸出對它們進行排序,然後。

許多TIA

回答

1

使用Enumerable#sort_by

試試這個

array_of_hashes.sort_by{|h| h["Duration"]} #=> change "Duration" any key you needed. 
+0

喜,這是行不通的哈希值仍然停止我的陣列 – 2013-04-25 11:20:47

+0

在同一位置上剛發現它不排序到位,但我必須排序到一個新的陣列。我還可以每天打印出不同的文本文件嗎? – 2013-04-25 12:35:23

+0

@NickWild什麼不排序它,我檢查。 – 2013-04-25 12:53:19