[ create a new paste ] login | about

Link: http://codepad.org/2TVCdqNJ    [ raw code | output | fork ]

Ruby, pasted on Jan 24:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def count_words(string)
    string.downcase!
    hash_str = Hash.new
    string.split(/\W+/i).each {|y| 
      if(hash_str.has_key?(y)) 
         hash_str[y] += 1
      else 
         hash_str[y]=1
      end
    }
    return hash_str
end

hash_t = count_words("A man, a plan, a canal -- Panama")
hash_t.each{|x,y| puts "#{x}:#{y}"}
puts
puts hash_t.inspect


Output:
1
2
3
4
5
6
7
a:3
plan:1
man:1
canal:1
panama:1

{"a"=>3, "plan"=>1, "man"=>1, "canal"=>1, "panama"=>1}


Create a new paste based on this one


Comments: