ruby - Parse and count structured text to build statistics -


i have specific problem can't find solution for. data in following format text file

 date1 reason1 date1 reason1 date1 reason2 date1 reason3 date2 reason4 date2 reason1 date2 reason2 date2 reason2 date2 reason1 date2 reason3 date3 reason4 date3 reason4 date3 reason1 

i want build statistics on data e.g. want count different "reasons" each date this

 date1 reason1 -> 2 date1 reason2 -> 1 date1 reason3 -> 1 date2 reason1 -> 2 date2 reason4 -> 1 date2 reason2 -> 2 date1 reason3 -> 1 

...and on. how parse data , build required result? guess hashes used failed imagine way solve this.

here's straight forward approach:

h = hash.new(0) file.foreach("foo.txt") |line| h[line.chomp] += 1 end h #=> {"date1 reason1"=>2, "date1 reason2"=>1, "date1 reason3"=>1, "date2 reason4"=>1, "date2 reason1"=>2, "date2 reason2"=>2, "date2 reason3"=>1, "date3 reason4"=>2, "date3 reason1"=>1} 

Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -