Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Hash counting

Reply
Thread Tools

Hash counting

 
 
Stuart Clarke
Guest
Posts: n/a
 
      02-02-2009
I am trying to load some data into a hash and then count how many times
it occurs in the hash, if it occurs more than 5 times then we are adding
some data to an array. Below is my code which I will explain

eventdateID.push eventsbydate.gsub(/\s/, '')[0..7] +
eventsbydate[26..30]
counts = Hash.new(0)
if eventdateID.find {|d| (counts[d] +=1) >= 5}
@alerts.push("#{event.event_id} #{@tab} #{event.time_written}
#{@tab}#{event.event_type} #{@tab} #{type}")
end

The first line loads a time and date value into an array and using gsub
it creates the date and time into an ID value. We then process the array
and say if an entry (a date/time ID) occurs more or equal to 5 times add
some data to an array.

My testing with this code is not picking up on any such occurrences,
which I no exist see below:

MonFeb022009
MonFeb022009
MonFeb022009
MonFeb022009
MonFeb022009
MonFeb022009
MonFeb022009
MonFeb022009
MonFeb022009

Does anyone have any ideas why my code is not working?

I do not get errors, it just does not return any data.

Thanks in advance
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Brian Candler
Guest
Posts: n/a
 
      02-02-2009
STDERR.puts is your friend.

> eventdateID.push eventsbydate.gsub(/\s/, '')[0..7] +
> eventsbydate[26..30]


STDERR.puts "A: #{eventdateID.inspect}"

> counts = Hash.new(0)
> if eventdateID.find {|d| (counts[d] +=1) >= 5}
> @alerts.push("#{event.event_id} #{@tab} #{event.time_written}
> #{@tab}#{event.event_type} #{@tab} #{type}")


STDERR.puts "B: #{@alerts.inspect}"

> end


Then you can see if the data is what you expect before you go into the
loop.

Note that 'find' will abort after one successful match. Is that what you
want?
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Ilan Berci
Guest
Posts: n/a
 
      02-02-2009
Stuart,

I believe this will get you closer to what you want..

[1,2,2,3,3,3].inject({}) do |hash, val|
hash[val] ||= 0
hash[val] +=1
hash
end

=> {1=>1,2=>2,3=>3}

hth

ilan


Stuart Clarke wrote:
> I am trying to load some data into a hash and then count how many times
> it occurs in the hash, if it occurs more than 5 times then we are adding
> some data to an array. Below is my code which I will explain
>
> eventdateID.push eventsbydate.gsub(/\s/, '')[0..7] +
> eventsbydate[26..30]
> counts = Hash.new(0)
> if eventdateID.find {|d| (counts[d] +=1) >= 5}
> @alerts.push("#{event.event_id} #{@tab} #{event.time_written}
> #{@tab}#{event.event_type} #{@tab} #{type}")
> end
>

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

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      02-02-2009
On 02.02.2009 21:43, Stuart Clarke wrote:
> I am trying to load some data into a hash and then count how many times
> it occurs in the hash, if it occurs more than 5 times then we are adding
> some data to an array. Below is my code which I will explain
>
> eventdateID.push eventsbydate.gsub(/\s/, '')[0..7] +
> eventsbydate[26..30]
> counts = Hash.new(0)
> if eventdateID.find {|d| (counts[d] +=1) >= 5}
> @alerts.push("#{event.event_id} #{@tab} #{event.time_written}
> #{@tab}#{event.event_type} #{@tab} #{type}")
> end


You probably rather want

eventdateID.push eventsbydate.gsub(/\s/, '')[0..7] +
eventsbydate[26..30]
counts = Hash.new(0)
eventdateID.each {|d| counts[d] +=1}
counts.each do |d,cnt|
@alerts.push("#{event.event_id} #{@tab} #{event.time_written}
#{@tab}#{event.event_type} #{@tab} #{type}") if cnt >= 5
end

Cheers

robert
 
Reply With Quote
 
Stuart Clarke
Guest
Posts: n/a
 
      02-02-2009
Thanks Robert.

This is more to what I need. However I am still getting no result,
everything works until we get to this section:

> counts = Hash.new(0)
> eventdateID.each {|d| counts[d] +=1}
> counts.each do |d,cnt|
> @alerts.push("#{event.event_id} #{@tab} #{event.time_written}
> #{@tab}#{event.event_type} #{@tab} #{type}") if cnt >= 5
> end


Does it make any difference that the data being read into the
eventdateID is alphanumeric eg:

MonFeb022009
MonFeb022009
MonFeb022009

Many thanks.


Robert Klemme wrote:
> On 02.02.2009 21:43, Stuart Clarke wrote:
>> end

> You probably rather want
>
> eventdateID.push eventsbydate.gsub(/\s/, '')[0..7] +
> eventsbydate[26..30]
> counts = Hash.new(0)
> eventdateID.each {|d| counts[d] +=1}
> counts.each do |d,cnt|
> @alerts.push("#{event.event_id} #{@tab} #{event.time_written}
> #{@tab}#{event.event_type} #{@tab} #{type}") if cnt >= 5
> end
>
> Cheers
>
> robert


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

 
Reply With Quote
 
Stuart Clarke
Guest
Posts: n/a
 
      02-02-2009
I have worked out the problem but I am a little unsure how to solve it.

We have counts which holds all of the event ID's, however |d, cnt| is
not counting the number of matching ID numbers and it just assigns each
ID the number 1.

So given this example, we would expect cnt to find the ID of ?? as
occuring more than 5 times and ignore the rest:

MonFeb022009
MonFeb022009
MonFeb022009
MonFeb022009
MonFeb022009
MonFeb022009
MonFeb022009
TueAug052008
TueAug052008
WedAug062008

However instead cnt is ust placing the number 1 for each ID for example

1
1
1
1
1
1
1
1
1
1

Can anyone help me with a fix? Many thanks

Stuart Clarke wrote:
> Thanks Robert.
>
> This is more to what I need. However I am still getting no result,
> everything works until we get to this section:
>
>> counts = Hash.new(0)
>> eventdateID.each {|d| counts[d] +=1}
>> counts.each do |d,cnt|
>> @alerts.push("#{event.event_id} #{@tab} #{event.time_written}
>> #{@tab}#{event.event_type} #{@tab} #{type}") if cnt >= 5
>> end

>
> Does it make any difference that the data being read into the
> eventdateID is alphanumeric eg:
>
> MonFeb022009
> MonFeb022009
> MonFeb022009
>
> Many thanks.
>
>
> Robert Klemme wrote:
>> On 02.02.2009 21:43, Stuart Clarke wrote:
>>> end

>> You probably rather want
>>
>> eventdateID.push eventsbydate.gsub(/\s/, '')[0..7] +
>> eventsbydate[26..30]
>> counts = Hash.new(0)
>> eventdateID.each {|d| counts[d] +=1}
>> counts.each do |d,cnt|
>> @alerts.push("#{event.event_id} #{@tab} #{event.time_written}
>> #{@tab}#{event.event_type} #{@tab} #{type}") if cnt >= 5
>> end
>>
>> Cheers
>>
>> robert


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

 
Reply With Quote
 
Stuart Clarke
Guest
Posts: n/a
 
      02-02-2009
I have worked out the problem but I am a little unsure how to solve it.

We have counts which holds all of the event ID's, however |d, cnt| is
not counting the number of matching ID numbers and it just assigns each
ID the number 1.

So given this example, we would expect cnt to find the ID of ?? as
occuring more than 5 times and ignore the rest:

MonFeb022009
MonFeb022009
MonFeb022009
MonFeb022009
MonFeb022009
MonFeb022009
MonFeb022009
TueAug052008
TueAug052008
WedAug062008

However instead cnt is just placing the number 1 for each ID for example

1
1
1
1
1
1
1
1
1
1

Can anyone help me with a fix? Many thanks

Stuart Clarke wrote:
> Thanks Robert.
>
> This is more to what I need. However I am still getting no result,
> everything works until we get to this section:
>
>> counts = Hash.new(0)
>> eventdateID.each {|d| counts[d] +=1}
>> counts.each do |d,cnt|
>> @alerts.push("#{event.event_id} #{@tab} #{event.time_written}
>> #{@tab}#{event.event_type} #{@tab} #{type}") if cnt >= 5
>> end

>
> Does it make any difference that the data being read into the
> eventdateID is alphanumeric eg:
>
> MonFeb022009
> MonFeb022009
> MonFeb022009
>
> Many thanks.
>
>
> Robert Klemme wrote:
>> On 02.02.2009 21:43, Stuart Clarke wrote:
>>> end

>> You probably rather want
>>
>> eventdateID.push eventsbydate.gsub(/\s/, '')[0..7] +
>> eventsbydate[26..30]
>> counts = Hash.new(0)
>> eventdateID.each {|d| counts[d] +=1}
>> counts.each do |d,cnt|
>> @alerts.push("#{event.event_id} #{@tab} #{event.time_written}
>> #{@tab}#{event.event_type} #{@tab} #{type}") if cnt >= 5
>> end
>>
>> Cheers
>>
>> robert


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

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      02-03-2009
2009/2/3 Stuart Clarke <(E-Mail Removed)>:
> I have worked out the problem but I am a little unsure how to solve it.
>
> We have counts which holds all of the event ID's, however |d, cnt| is
> not counting the number of matching ID numbers and it just assigns each
> ID the number 1.


What does that mean? What's in the Hash?

> So given this example, we would expect cnt to find the ID of ?? as
> occuring more than 5 times and ignore the rest:
>
> MonFeb022009
> MonFeb022009
> MonFeb022009
> MonFeb022009
> MonFeb022009
> MonFeb022009
> MonFeb022009
> TueAug052008
> TueAug052008
> WedAug062008
>
> However instead cnt is ust placing the number 1 for each ID for example
>
> 1
> 1
> 1
> 1
> 1
> 1
> 1
> 1
> 1
> 1
>
> Can anyone help me with a fix? Many thanks


Frankly, you lost me there. Please do this:

require 'pp'

File.open('/tmp/log', 'w') {|io| io.write(counts.pretty_inspect)}

And look at the output and / or post it here.

robert

--
remember.guy do |as, often| as.you_can - without end

 
Reply With Quote
 
Stuart Clarke
Guest
Posts: n/a
 
      02-03-2009
Thanks for replying and sorry for the confusion.

My hash (counts) contains date and time ID's like so TueAug052008

When I do a puts on counts I get a list of these as per there date and
time values which is what I want. However counting to see if there is
more than 5 occurances of one the ID values fails and doesn't find
anything in my data set.

I have done as you asked and the output is as follows:

{"WedAug062008"=>1}

This suggests there is a problem. Just for your information doing an
output on counts (the hash) gives this:

MonFeb0220091
MonFeb0220091
MonFeb0220091
MonFeb0220091
MonFeb0220091
MonFeb0220091
MonFeb0220091
MonFeb0220091
WedAug062008
WedAug062008

Thanks for your help


Robert Klemme wrote:
> 2009/2/3 Stuart Clarke <(E-Mail Removed)>:
>> I have worked out the problem but I am a little unsure how to solve it.
>>
>> We have counts which holds all of the event ID's, however |d, cnt| is
>> not counting the number of matching ID numbers and it just assigns each
>> ID the number 1.

>
> What does that mean? What's in the Hash?
>
>> TueAug052008
>> 1
>> 1
>> 1
>> 1
>> 1
>>
>> Can anyone help me with a fix? Many thanks

>
> Frankly, you lost me there. Please do this:
>
> require 'pp'
>
> File.open('/tmp/log', 'w') {|io| io.write(counts.pretty_inspect)}
>
> And look at the output and / or post it here.
>
> robert


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

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      02-03-2009
2009/2/3 Stuart Clarke <(E-Mail Removed)>:
> Thanks for replying and sorry for the confusion.
>
> My hash (counts) contains date and time ID's like so TueAug052008


Obviously not as the output below demonstrates that there is just a
single entry in the Hash.

> When I do a puts on counts I get a list of these as per there date and
> time values which is what I want. However counting to see if there is
> more than 5 occurances of one the ID values fails and doesn't find
> anything in my data set.
>
> I have done as you asked and the output is as follows:
>
> {"WedAug062008"=>1}


Looks like there is a lot missing.

> This suggests there is a problem. Just for your information doing an
> output on counts (the hash) gives this:


What does "doing an output" mean? Please be more specific (e.g. by
posting complete code, ideally a test case that someone else can
execute), otherwise nobody can help you.

> MonFeb0220091
> MonFeb0220091
> MonFeb0220091
> MonFeb0220091
> MonFeb0220091
> MonFeb0220091
> MonFeb0220091
> MonFeb0220091
> WedAug062008
> WedAug062008


Cheers

robert

--
remember.guy do |as, often| as.you_can - without end

 
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
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Counting values in an array, storing in a hash then making an arrayof hashes? Jen Ruby 3 03-23-2011 12:11 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
counting up instead of counting down edwardfredriks Javascript 6 09-07-2005 03:30 PM
Counting elements in a hash russell.brooks@perdue.com Perl Misc 4 06-10-2004 01:40 PM



Advertisments