From: Peter Szinek Date: 2008-04-26T22:57:04+09:00 Subject: Re: All occurances of a character in a string --Apple-Mail-1--1017213961 Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes Content-Transfer-Encoding: 7bit Hi Raghu My 2-minute horrible-hack(tm): input = "abbccdaab" i = 0; input.split('').inject({}) {|a,v| a[v] = a[v] ? (a[v] << input[i..-1].index(v)+i) : [input[i..-1].index(v)+i]; i+=1; a} {"a"=>[0, 6, 7], "b"=>[1, 2, 8], "c"=>[3, 4], "d"=>[5]} I am sure there has to be a *MUCH* nicer way though, because this is rather ugly... Cheers, Peter ___ http://www.rubyrailways.com http://scrubyt.org On Apr 26, 2008, at 2: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/. > --Apple-Mail-1--1017213961--