String comparisions and counting

S

Stuart Clarke

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
 
T

Todd Benson

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

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

Todd
 
H

Harry Kakueki

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
 
B

Brian Candler

Stuart said:
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
 
H

Harry Kakueki

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

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
 
T

Todd Benson

Stuart said:
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
--

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

Todd
 
S

Stuart Clarke

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

Many thanks

Brian said:
Stuart said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top