[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Arary : count duplicated object

Josselin

6/29/2006 9:42:00 AM

I look into an array booked_days to see if an object d (which is a
date object) is included using :

if @booked_days.include?(d)

result is true/false, fine...
but is there any way to know if the object (d) is duplicated in the array ?

something like

if @booked_days.duplicated?(d) (getting true/false)
or
if @booked_days.count?(d) (getting the number of duplicated objects)

joss

7 Answers

Robert Klemme

6/29/2006 9:45:00 AM

0

Josselin wrote:
> I look into an array booked_days to see if an object d (which is a date
> object) is included using :
>
> if @booked_days.include?(d)
>
> result is true/false, fine...
> but is there any way to know if the object (d) is duplicated in the
> array ?
>
> something like
>
> if @booked_days.duplicated?(d) (getting true/false)
> or
> if @booked_days.count?(d) (getting the number of duplicated objects)

@booked_days.inject(0) {|cnt, el| d == el ? cnt.succ : cnt}
@booked_days.select {|el| d == el}.size

HTH

Kind regards

robert

Josselin

6/29/2006 3:25:00 PM

0

On 2006-06-29 11:45:29 +0200, Robert Klemme <bob.news@gmx.net> said:

> Josselin wrote:
>> I look into an array booked_days to see if an object d (which is a
>> date object) is included using :
>>
>> if @booked_days.include?(d)
>>
>> result is true/false, fine...
>> but is there any way to know if the object (d) is duplicated in the array ?
>>
>> something like
>>
>> if @booked_days.duplicated?(d) (getting true/false)
>> or
>> if @booked_days.count?(d) (getting the number of duplicated objects)
>
> @booked_days.inject(0) {|cnt, el| d == el ? cnt.succ : cnt}
> @booked_days.select {|el| d == el}.size
>
> HTH
>
> Kind regards
>
> robert

thanks a lot... Robert !
As all newrubies, I'll spend the night to translate it (in order to
ameliorate my rating...)
my interest for Ruby is growing everytime I see how powerful it can be !!

Robert Klemme

7/2/2006 9:13:00 AM

0

Josselin <josselin@wanadoo.fr> wrote:
> thanks a lot... Robert !
> As all newrubies, I'll spend the night to translate it (in order to
> ameliorate my rating...)

Did you succeed or rather want some comments in the code?

> my interest for Ruby is growing everytime I see how powerful it can
> be !!

:-)

PS: Here's an even more general solution that counts occurrences of *all*
elements in an Enumerable:

counts = enum.inject(Hash.new(0)) {|cnt,e| cnt[e]+=1; cnt}

Kind regards

robert

Josselin

7/3/2006 5:53:00 AM

0

On 2006-07-02 11:13:29 +0200, "Robert Klemme" <bob.news@gmx.net> said:

> Josselin <josselin@wanadoo.fr> wrote:
>> thanks a lot... Robert !
>> As all newrubies, I'll spend the night to translate it (in order to
>> ameliorate my rating...)
>
> Did you succeed or rather want some comments in the code?
>
>> my interest for Ruby is growing everytime I see how powerful it can
>> be !!
>
> :-)
>
> PS: Here's an even more general solution that counts occurrences of
> *all* elements in an Enumerable:
>
> counts = enum.inject(Hash.new(0)) {|cnt,e| cnt[e]+=1; cnt}
>
> Kind regards
>
> robert

running fine, but had no time actually to look into the code (soccer
world cup, France-Brasil !)

Robert Klemme

7/3/2006 7:27:00 AM

0

Josselin wrote:
> running fine, but had no time actually to look into the code (soccer
> world cup, France-Brasil !)

:-) Congrats! Now there's only old and new Europe left in the cup...
At the moment I consider the French to be favorite but I keep my fingers
crossed for the Germans of course.

Kind regards

robert

Josselin

7/3/2006 9:51:00 AM

0

On 2006-07-03 09:26:31 +0200, Robert Klemme <bob.news@gmx.net> said:

> Josselin wrote:
>> running fine, but had no time actually to look into the code (soccer
>> world cup, France-Brasil !)
>
> :-) Congrats! Now there's only old and new Europe left in the cup...
> At the moment I consider the French to be favorite but I keep my
> fingers crossed for the Germans of course.
>
> Kind regards
>
> robert

Even if the French team is more 'experienced' I still consider Germany
has the favorite beacuse playing home (as the French in 98..) and more
I'd like the Germans get the same feeling we got in 98... they deserve
it ! I consider this cup as beautiful as 98 because of the Germans
people behavior... Germany-France will be my favorite final !!!

as you have a lot of Ruby knowledge , I would like to 'exchange'
dot-commas in a string

s= "123.456,78" -> "123,456.78"
I now I should use a s.gsub with a block, but I don't know actually
how to write it... (I can write 3 gsubs.... but it would be ugly.....)

joss



Robert Klemme

7/3/2006 9:58:00 AM

0

Josselin wrote:
> On 2006-07-03 09:26:31 +0200, Robert Klemme <bob.news@gmx.net> said:
>
>> Josselin wrote:
>>> running fine, but had no time actually to look into the code (soccer
>>> world cup, France-Brasil !)
>>
>> :-) Congrats! Now there's only old and new Europe left in the cup...
>> At the moment I consider the French to be favorite but I keep my
>> fingers crossed for the Germans of course.
>>
>> Kind regards
>>
>> robert
>
> Even if the French team is more 'experienced' I still consider Germany
> has the favorite beacuse playing home (as the French in 98..) and more
> I'd like the Germans get the same feeling we got in 98... they deserve
> it ! I consider this cup as beautiful as 98 because of the Germans
> people behavior... Germany-France will be my favorite final !!!
>
> as you have a lot of Ruby knowledge , I would like to 'exchange'
> dot-commas in a string
>
> s= "123.456,78" -> "123,456.78"
> I now I should use a s.gsub with a block, but I don't know actually
> how to write it... (I can write 3 gsubs.... but it would be ugly.....)

>> "123.456,78".gsub(/[.,]/) {|m| m == "." ? "," : "."}
=> "123,456.78"

But his is far better:

>> "123.456,78".tr ".,", ",."
=> "123,456.78"

Cheers

robert