From: Harry Kakueki Date: 2008-05-01T00:23:21+09:00 Subject: Re: All occurances of a character in a string On Sat, Apr 26, 2008 at 9:55 PM, Raghu Go wrote: > Hi - I am trying to figure out the ruby way of printing all occurances > of a character in a string.. > > The index method on string gives only the first occurance only. > > Example input string - abbccdaab > desired output - 'a' = 0 6 7 > 'b' = 1 2 8 > 'c' = 3 4 > 'd' = 5 > > Any help greatly appreciated.. > -- > Posted via http://www.ruby-forum.com/. > > Here is one way. arr = "abbccdaab".split(//) arr.uniq.each do |s| res = [] (0...arr.length).each {|x| res << x if arr[x] =~ /#{s}/} p s, res end Harry -- A Look into Japanese Ruby List in English http://www.kakueki.com/ruby/list.html