[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

how to group visitors using hours

Ayeye Brazov

3/2/2009 12:12:00 PM

Hi,

I've got this problem, I have a Visitor class that keep tracks
of visitors. Each visitor has "created_at" field.
I'd like to group them by hours using "created_at"

I came up with this solution, but I'm not very happy about it...

First, I create a list having ["yy-mm-dd hh",visitor.id], with

list = visitors.map{|i| [i.created_at.strftime("%Y-%m-%d %H"),
i.id]}

Then, I group each visitor depending on the hours using an Hash

hs= {}
for l in list
if hs.has_key?(l.first)
hs[l.first] << l.second # Use the hour as index, and append the
visitor id
else
hs[l.first] = [l.second]
end
end

The result is a Hash hs, and I can use it to get the visitors
given a certain hour.

However, I'm sure there's a better way of doing it, especially if
I'd like to group visitors for "months" or "years"

Any suggestions please?!
thanks
b.
--
Posted via http://www.ruby-....

2 Answers

Michael Fellinger

3/2/2009 12:26:00 PM

0

On Mon, Mar 2, 2009 at 9:12 PM, Ayeye Brazov <brazovayeye@yahoo.com> wrote:
> Hi,
>
> I've got this problem, I have a Visitor class that keep tracks
> of visitors. Each visitor has "created_at" field.
> I'd like to group them by hours using "created_at"
>
> I came up with this solution, but I'm not very happy about it...
>
> First, I create a list having ["yy-mm-dd hh",visitor.id], with
>
> =C2=A0 =C2=A0list =3D visitors.map{|i| [i.created_at.strftime("%Y-%m-%d %=
H"),
> i.id]}
>
> Then, I group each visitor depending on the hours using an Hash
>
> =C2=A0 hs=3D {}
> =C2=A0 =C2=A0for l in list
> =C2=A0 =C2=A0 =C2=A0if hs.has_key?(l.first)
> =C2=A0 =C2=A0 =C2=A0 =C2=A0hs[l.first] << l.second # Use the hour as inde=
x, and append the
> visitor id
> =C2=A0 =C2=A0 =C2=A0else
> =C2=A0 =C2=A0 =C2=A0 =C2=A0hs[l.first] =3D [l.second]
> =C2=A0 =C2=A0 =C2=A0end
> =C2=A0 =C2=A0end
>
> The result is a Hash hs, and I can use it to get the visitors
> given a certain hour.
>
> However, I'm sure there's a better way of doing it, especially if
> I'd like to group visitors for "months" or "years"
>
> Any suggestions please?!
> thanks
> b.
> --
> Posted via http://www.ruby-....
>
>

require 'pp'

class Visitor
attr_accessor :created_at

def initialize(time)
@created_at =3D time
end
end

visitors =3D Array.new(10){ Visitor.new(Time.at(rand(2e9))) }

pp visitors.group_by{|v| v.created_at.hour }
{6=3D>
[#<Visitor:0xb7bc034c @created_at=3DTue Jul 04 06:22:51 +0900 1978>,
#<Visitor:0xb7bc00a4 @created_at=3DThu Jul 26 06:27:32 +0900 1979>],
18=3D>
[#<Visitor:0xb7bc0194 @created_at=3DWed Jun 27 18:31:17 +0900 2018>,
#<Visitor:0xb7bbffdc @created_at=3DSun Oct 03 18:37:50 +0900 1993>],
13=3D>[#<Visitor:0xb7bc0108 @created_at=3DWed Feb 10 13:20:34 +0900 2027>]=
,
2=3D>
[#<Visitor:0xb7bc0158 @created_at=3DFri Jan 10 02:22:01 +0900 2003>,
#<Visitor:0xb7bc0054 @created_at=3DSun May 30 02:15:58 +0900 1993>],
9=3D>[#<Visitor:0xb7bbff8c @created_at=3DTue Oct 05 09:32:10 +0900 2032>],
4=3D>[#<Visitor:0xb7bc0388 @created_at=3DMon May 01 04:03:29 +0900 2006>],
10=3D>[#<Visitor:0xb7bc002c @created_at=3DMon Jun 27 10:28:29 +0900 1994>]=
}

^ manveru

Ayeye Brazov

3/2/2009 2:38:00 PM

0

Thanks for that!
b.
--
Posted via http://www.ruby-....