Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > String comparisions and counting

Reply
Thread Tools

String comparisions and counting

 
 
Stuart Clarke
Guest
Posts: n/a
 
      11-05-2008
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-forum.com/.

 
Reply With Quote
 
 
 
 
Todd Benson
Guest
Posts: n/a
 
      11-05-2008
On Wed, Nov 5, 2008 at 7:35 AM, Stuart Clarke
<(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Harry Kakueki
Guest
Posts: n/a
 
      11-05-2008
On Wed, Nov 5, 2008 at 10:35 PM, Stuart Clarke
<(E-Mail Removed)> 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-forum.com/.
>
>




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

 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      11-05-2008
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-forum.com/.

 
Reply With Quote
 
Harry Kakueki
Guest
Posts: n/a
 
      11-05-2008
On Wed, Nov 5, 2008 at 10:35 PM, Stuart Clarke
<(E-Mail Removed)> 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-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

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

 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      11-05-2008
Oops:

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


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

 
Reply With Quote
 
Todd Benson
Guest
Posts: n/a
 
      11-05-2008
On Wed, Nov 5, 2008 at 8:18 AM, Brian Candler <(E-Mail Removed)> 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

 
Reply With Quote
 
Stuart Clarke
Guest
Posts: n/a
 
      11-05-2008
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-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointer comparisions of same type T Ryi C Programming 4 03-24-2010 06:43 PM
String comparisions Talib Hussain Ruby 4 02-12-2009 01:13 PM
parser performance comparisions Eric Mahurin Ruby 9 11-06-2005 12:55 AM
Counting occurances of string A in string B, and adding it to string B Sandman Perl Misc 7 08-03-2004 08:46 PM
Synthesis Tool Device Support Comparisions? td VHDL 0 09-13-2003 04:18 PM



Advertisments