# String comparisions and counting

**URL:** <https://www.ruby-forum.com/t/string-comparisions-and-counting/152605>\
**Category:** Ruby\
**Created:** [November 5, 2008, 2:37pm UTC](https://www.ruby-forum.com/t/string-comparisions-and-counting/152605 "2008-11-05T14:37:13Z")\
**Posts on this page:** 8\
**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:** [November 5, 2008, 2:37pm UTC](https://www.ruby-forum.com/t/string-comparisions-and-counting/152605/1 "2008-11-05T14:37:13Z")

</div>

I have an array full of strings which represent a date ID. The array  
contains indivduals strings like the following:

TueAug052008

I want to iterate through this array (@eventbydate[]) and check each of  
the values of the array. I then want a statement which says if any of  
the date ID’s in the array occurs more than 5 times print out some data.

Pseudo code

if dataID occurs more than 5times  
print results  
end

I hope this makes sense.

I would appreciate any help

Regards

---

<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:** [November 5, 2008, 3:03pm UTC](https://www.ruby-forum.com/t/string-comparisions-and-counting/152605/2 "2008-11-05T15:03:55Z")

</div>

On Wed, Nov 5, 2008 at 7:35 AM, Stuart C.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> if dataID occurs more than 5times  
> print results  
> end
> 
> I hope this makes sense.
> 
> I would appreciate any help
> 
> Regards

[1, 1, 2, 3, 4].count(1)

Todd

---

<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:** [November 5, 2008, 3:18pm UTC](https://www.ruby-forum.com/t/string-comparisions-and-counting/152605/3 "2008-11-05T15:18:46Z")

</div>

Stuart C. wrote:

> I have an array full of strings which represent a date ID. The array  
> contains indivduals strings like the following:
> 
> TueAug052008
> 
> I want to iterate through this array (@eventbydate[]) and check each of  
> the values of the array. I then want a statement which says if any of  
> the date ID’s in the array occurs more than 5 times print out some data.

counts = Hash.new(0)  
@eventbydate.each { |e| counts[e] += 1 }  
if counts.find { |c| c \>= 5 }  
puts “Print out some data”  
end

There are other variations:

…  
if counts.values.max \>= 5  
…

More efficient is to stop counting as soon as you reach 5, if you don’t  
need the final values:

counts = Hash.new(0)  
if @eventbydate.find { |e| (counts[e] += 1) \>= 5 }  
puts “Print out some data”  
end

---

<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:** [November 5, 2008, 3:20pm UTC](https://www.ruby-forum.com/t/string-comparisions-and-counting/152605/4 "2008-11-05T15:20:37Z")

</div>

On Wed, Nov 5, 2008 at 10:35 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:** [November 5, 2008, 3:20pm UTC](https://www.ruby-forum.com/t/string-comparisions-and-counting/152605/5 "2008-11-05T15:20:22Z")

</div>

Oops:

> if counts.find { |c| c \>= 5 }

if counts.find { |k,v| c \>= 5 }

---

<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:** [November 5, 2008, 3:21pm UTC](https://www.ruby-forum.com/t/string-comparisions-and-counting/152605/6 "2008-11-05T15:21:47Z")

</div>

On Wed, Nov 5, 2008 at 10:35 PM, Stuart C.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Is this helpful?

arr = [“a”,“b”,“a”,“c”,“a”,“a”,“c”]

h = Hash.new(0)  
arr.each {|x| h[x] += 1}  
h.each {|x,y| p x if y \> 3}

Harry

---

<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:** [November 5, 2008, 3:49pm UTC](https://www.ruby-forum.com/t/string-comparisions-and-counting/152605/7 "2008-11-05T15:49:31Z")

</div>

Great stuff thanks. I went the last solution as I only want to pick up  
any any high occurances.

Many thanks

Brian C. wrote:

> Stuart C. wrote:
> 
> > I have an array full of strings which represent a date ID. The array  
> > contains indivduals strings like the following:
> > 
> > TueAug052008
> > 
> > I want to iterate through this array (@eventbydate[]) and check each of  
> > the values of the array. I then want a statement which says if any of  
> > the date ID’s in the array occurs more than 5 times print out some data.
> 
> counts = Hash.new(0)  
> @eventbydate.each { |e| counts[e] += 1 }  
> if counts.find { |c| c \>= 5 }  
> puts “Print out some data”  
> end
> 
> There are other variations:
> 
> …  
> if counts.values.max \>= 5  
> …
> 
> More efficient is to stop counting as soon as you reach 5, if you don’t  
> need the final values:
> 
> counts = Hash.new(0)  
> if @eventbydate.find { |e| (counts[e] += 1) \>= 5 }  
> puts “Print out some data”  
> end

---

<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:** [November 5, 2008, 3:30pm UTC](https://www.ruby-forum.com/t/string-comparisions-and-counting/152605/8 "2008-11-05T15:30:02Z")

</div>

On Wed, Nov 5, 2008 at 8:18 AM, Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> counts = Hash.new(0)
> 
> More efficient is to stop counting as soon as you reach 5, if you don’t  
> need the final values:
> 
> ## counts = Hash.new(0) if @eventbydate.find { |e| (counts[e] += 1) \>= 5 } puts “Print out some data” end

Pretty darn good. Why not use a database? I guess it comes down to  
often you want to query the data.

Todd
