[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

String comparisions and counting

Stuart Clarke

11/5/2008 1:36:00 PM

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
--
Posted via http://www.ruby-....

7 Answers

Todd Benson

11/5/2008 2:02:00 PM

0

On Wed, Nov 5, 2008 at 7:35 AM, Stuart Clarke
<stuart.clarke1986@gmail.com> 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.
>
> 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

Harry Kakueki

11/5/2008 2:15:00 PM

0

On Wed, Nov 5, 2008 at 10:35 PM, Stuart Clarke
<stuart.clarke1986@gmail.com> 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.
>
> Pseudo code
>
> if dataID occurs more than 5times
> print results
> end
>
> I hope this makes sense.
>
> I would appreciate any help
>
> Regards
> --
> Posted via http://www.ruby-....
>
>



--
A Look into Japanese Ruby List in English
http://www.kakueki.com/ruby...

Brian Candler

11/5/2008 2:18:00 PM

0

Stuart Clarke 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
--
Posted via http://www.ruby-....

Harry Kakueki

11/5/2008 2:18:00 PM

0

On Wed, Nov 5, 2008 at 10:35 PM, Stuart Clarke
<stuart.clarke1986@gmail.com> 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.
>
> Pseudo code
>
> if dataID occurs more than 5times
> print results
> end
>
> I hope this makes sense.
>
> I would appreciate any help
>
> Regards
> --
> Posted via http://www.ruby-....
>
>

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

--
A Look into Japanese Ruby List in English
http://www.kakueki.com/ruby...

Brian Candler

11/5/2008 2:19:00 PM

0

Oops:

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

if counts.find { |k,v| c >= 5 }
--
Posted via http://www.ruby-....

Todd Benson

11/5/2008 2:28:00 PM

0

On Wed, Nov 5, 2008 at 8:18 AM, Brian Candler <b.candler@pobox.com> wrote:
> Stuart Clarke 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
> --

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

Todd

Stuart Clarke

11/5/2008 2:48:00 PM

0

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

Many thanks

Brian Candler wrote:
> Stuart Clarke 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

--
Posted via http://www.ruby-....