From: Miles Georgi Date: 2008-12-16T10:28:49+09:00 Subject: Re: Struggling with array searches/queries I'd just map the names in a hash. Something like name_hash = {} then in the loop/if statements: name_hash[name] ||= 0 name_hash[name] +=1 when you want to print it out name_hash.each_pair {|name,count| puts "#{key} occurs #{count} times"} I don't really understand the logic of your code. I'm not sure what the find method does, but it looks like your code would increment the count for everyname encountered so far if the ID is 123. I think you're experiencing a problem with your algorithm, not your code. Your algorithm doesn't seem to make sense to me. On Mon, Dec 15, 2008 at 3:31 PM, Stuart Clarke wrote: > I am reading delimited lines of data stored in array eg > > 123 name address date product > > I perform an array.each and then run an if statement based on the ID > (123). What I wish to do now is say > > if you find ID 123 > read the entries in name and count unique names > > eg the entries bob bob mike sue > Would report 2 bob > 1 mike > 1 sue > > I was thinking something like this > > if id==123 > namelist.push name > counts = Hash.new(0) > if namelist.find {|n| (counts[n]) +=1} > puts counts + name > > Obviously the coding is not correct, but can anyone comment or expand > further on this? > > Many thanks > -- > Posted via http://www.ruby-forum.com/. > >