# Struggling with array searches/queries

**URL:** <https://www.ruby-forum.com/t/struggling-with-array-searches-queries/155724>\
**Category:** Ruby\
**Created:** [December 16, 2008, 12:39am UTC](https://www.ruby-forum.com/t/struggling-with-array-searches-queries/155724 "2008-12-16T00:39:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sclarke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sclarke](https://www.ruby-forum.com/u/sclarke)\
**Post date:** [December 16, 2008, 12:39am UTC](https://www.ruby-forum.com/t/struggling-with-array-searches-queries/155724/1 "2008-12-16T00:39:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sclarke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sclarke](https://www.ruby-forum.com/u/sclarke)\
**Post date:** [December 16, 2008, 2:37am UTC](https://www.ruby-forum.com/t/struggling-with-array-searches-queries/155724/2 "2008-12-16T02:37:04Z")

</div>

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 C.

---

<div class="post-metadata">

**Author:** ![sclarke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sclarke](https://www.ruby-forum.com/u/sclarke)\
**Post date:** [December 16, 2008, 3:29am UTC](https://www.ruby-forum.com/t/struggling-with-array-searches-queries/155724/3 "2008-12-16T03:29:08Z")

</div>

On Mon, Dec 15, 2008 at 5:31 PM, Stuart C.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> eg the entries bob bob mike sue  
> puts counts + name
> 
> Obviously the coding is not correct, but can anyone comment or expand  
> further on this?

An approach…

a = %w(bob bob mike sue)  
p a.inject(Hash.new(0)) {|h, n| h[n] += 1; h}  
=\> {“mike”=\>1, “sue”=\>1, “bob”=\>2}

…or (in 1.8.7) a different one…

a = %w(bob bob mike sue)  
p a.uniq.inject({}) {|h, i| h[i] = a.count(i); h}

Todd
