[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Hash counting

Stuart Clarke

2/2/2009 8:44:00 PM

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-....

23 Answers

Evolution

1/24/2009 4:12:00 AM

0

Joe wrote:
> On Jan 23, 5:02 pm, gumboman <gu...@gumbo.com> wrote:
>> Hey, I always said it was 50 - 50. You guys said those WMD's were
>> 100%.
>
> Not me. I never cared about WMD's. I just wanted to see them kick
> Sadaam's ass. Mission Accomplished!

Wow... 100s of thousands dead, and you think that's worth kicking
Saddam's ass. You have some strange values.

--
Laurie

http://lauriehester.blo...

hantayo

1/24/2009 8:14:00 PM

0


"Evolution" <myname@rcn.com> wrote in message
news:xbKdnd_zltv2zufUnZ2dnUVZ_uOdnZ2d@wavecable.com...
> Joe wrote:
>> On Jan 23, 1:07 pm, Evolution <myn...@rcn.com> wrote:
>>> I wonder if bush was even aware of all that was done in his name by
>>> cheney at the last minute. Seriously. How can someone be so puppy-dog
>>> like around Obama and so happy, knowing he'd given the go-ahead to allow
>>> animal waste in our water? I don't think even balloon or longyard would
>>> be able to justify that one...
>>
>> Our water comes from the Delaware River. That's also where our turds
>> get dumped. We're supposed to worry about a little dog poop?
>>
>> You need to lighten up....
>
> We're not talking dog poop here... ever seen a factory farm?
>
> Why do you think they keep having to recall produce because of salmonella
> poisoning?
>
> --
> Laurie
>
> http://lauriehester.blo...


Saying nothing about the treatment of the critters. God - I hate the way
they make the critters exist. They (the critters or prisoners I should
say...) are in a living hell! Puppy mills, factory farms - they are all the
same! Sorry to get up onto my soapbox....:-)
Kathy K.


Brian Candler

2/2/2009 8:50:00 PM

0

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-....

Ilan Berci

2/2/2009 8:59:00 PM

0

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-....

Robert Klemme

2/2/2009 9:24:00 PM

0

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

Stuart Clarke

2/2/2009 10:38:00 PM

0

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-....

Stuart Clarke

2/2/2009 11:37:00 PM

0

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-....

Stuart Clarke

2/2/2009 11:37:00 PM

0

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-....

Robert Klemme

2/3/2009 9:56:00 AM

0

2009/2/3 Stuart Clarke <stuart.clarke1986@gmail.com>:
> 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

Stuart Clarke

2/3/2009 4:15:00 PM

0

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 <stuart.clarke1986@gmail.com>:
>> 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-....