[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Can i calculate the data number in array?

Cool Wong

6/28/2007 8:30:00 AM

[code]
Array = ["a", "a", "a", "b", "b", â??câ?, "c", "c","d", "d", "e"]
[/code]


Can i calculate the data in the Array???

a a a b b c c c d d e

For example: ArrayNumber = ["3", "2", "3", "2", "1"]

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

8 Answers

gz zz

6/28/2007 8:42:00 AM

0

d=["a", "a", "a", "b", "b", "c", "c", "c","d", "d", "e"]
p d

dd={}
d.each do |v|
dd[v]||=0
dd[v]+=1
end

p dd

p dd.values


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

Axel Etzold

6/28/2007 8:45:00 AM

0

Dear Cool,

maybe this is what you want:

class Array



def count

k=Hash.new(0)

self.each{ |x| k[x]+=1 }

k

end



end


my_array = ["a", "a", "a", "b", "b", â??câ?, "c", "c","d", "d", "e"]
my_array.count

It gives you a Hash, though, and the numbers are Integers,
not Strings, but this seems more reasonable :)

Best regards,

Axel
--
GMX FreeMail: 1 GB Postfach, 5 E-Mail-Adressen, 10 Free SMS.
Alle Infos und kostenlose Anmeldung: http://www.gmx.net/de/g...

gz zz

6/28/2007 8:45:00 AM

0

gz zz wrote:
> d=["a", "a", "a", "b", "b", "c", "c", "c","d", "d", "e"]
> p d
>
> dd={}
> d.each do |v|
> dd[v]||=0
> dd[v]+=1
> end
>
> p dd
>
> p dd.values

But,I think ruby's Hash is not ordered,so...

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

Robert Klemme

6/28/2007 10:07:00 AM

0

On 28.06.2007 10:44, Axel Etzold wrote:
> Dear Cool,
>
> maybe this is what you want:
>
> class Array
>
>
>
> def count
>
> k=Hash.new(0)
>
> self.each{ |x| k[x]+=1 }
>
> k
>
> end
>
>
>
> end
>
>
> my_array = ["a", "a", "a", "b", "b", “c”, "c", "c","d", "d", "e"]
> my_array.count
>
> It gives you a Hash, though, and the numbers are Integers,
> not Strings, but this seems more reasonable :)

Yes, I'd agree. Having tuples as return value is better because then
you maintain relationship between items and item count.

Btw, thanks for leaving the inject solution to me. :-)

irb(main):005:0> a = %w{a a a b b c c c d d e}
=> ["a", "a", "a", "b", "b", "c", "c", "c", "d", "d", "e"]
irb(main):006:0> a.inject(Hash.new(0)) {|h,x| h[x]+=1; h}
=> {"a"=>3, "b"=>2, "c"=>3, "d"=>2, "e"=>1}
irb(main):007:0> a.inject(Hash.new(0)) {|h,x| h[x]+=1; h}.sort
=> [["a", 3], ["b", 2], ["c", 3], ["d", 2], ["e", 1]]

Kind regards

robert

Robert Dober

6/28/2007 10:56:00 AM

0

On 6/28/07, Robert Klemme <shortcutter@googlemail.com> wrote:

> Btw, thanks for leaving the inject solution to me. :-)
Shame on you!!! ;)
But that is not what OP asked for <G>

(a|[]).map{|e|a.grep(e).size} ### I know it might not work as we use #=== here

More generally we have therefore

(a|[]).map{|e|a.select{|x|x==e}.size}

Maybe OP preferred
...).sort.map{...

and sorry OP, I just needed this working break.

Robert
--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

Robert Klemme

6/28/2007 11:46:00 AM

0

On 28.06.2007 12:56, Robert Dober wrote:
> On 6/28/07, Robert Klemme <shortcutter@googlemail.com> wrote:
>
>> Btw, thanks for leaving the inject solution to me. :-)
> Shame on you!!! ;)

Um, why? Somehow I don't get the joke, sorry. :-)

> But that is not what OP asked for <G>
>
> (a|[]).map{|e|a.grep(e).size} ### I know it might not work as we use
> #=== here

Sorry, Robert, but I don't think this is what the OP was asking for:

irb(main):001:0> a = %w{a a a b b c c c d d e}
=> ["a", "a", "a", "b", "b", "c", "c", "c", "d", "d", "e"]
irb(main):002:0> a.map{|e|a.grep(e).size}
=> [3, 3, 3, 2, 2, 3, 3, 3, 2, 2, 1]

Your solution repeats counts for the same string but as far as I can see
from his original posting he wants counts for each string just once.

> More generally we have therefore
>
> (a|[]).map{|e|a.select{|x|x==e}.size}
>
> Maybe OP preferred
> ..).sort.map{...
>
> and sorry OP, I just needed this working break.

:-)

Kind regards

robert

Robert Dober

6/28/2007 2:12:00 PM

0

On 6/28/07, Robert Klemme <shortcutter@googlemail.com> wrote:
> On 28.06.2007 12:56, Robert Dober wrote:
> > On 6/28/07, Robert Klemme <shortcutter@googlemail.com> wrote:
> >
> >> Btw, thanks for leaving the inject solution to me. :-)
> > Shame on you!!! ;)
>
> Um, why? Somehow I don't get the joke, sorry. :-)
Really or are you metajoking?anyway to get out of recursive joke mode,
we are sometimes competing about #inject.
>
> > But that is not what OP asked for <G>
> >
> > (a|[]).map{|e|a.grep(e).size} ### I know it might not work as we use
> > #=== here
>
> Sorry, Robert, but I don't think this is what the OP was asking for:
>
> irb(main):001:0> a = %w{a a a b b c c c d d e}
> => ["a", "a", "a", "b", "b", "c", "c", "c", "d", "d", "e"]
> irb(main):002:0> a.map{|e|a.grep(e).size}
> => [3, 3, 3, 2, 2, 3, 3, 3, 2, 2, 1]
Careful here Robert I wrote

(a|[]) which is a more computation intensive way to write a.uniq. --
its a coding joke brrr

Read my post please you can learn sooooo much. -- this is a joke!

Robert

--
I always knew that one day Smalltalk would replace Java.
I just didn't know it would be called Ruby
-- Kent Back

Robert Klemme

6/28/2007 9:19:00 PM

0

On 28.06.2007 16:11, Robert Dober wrote:
> On 6/28/07, Robert Klemme <shortcutter@googlemail.com> wrote:
>> On 28.06.2007 12:56, Robert Dober wrote:
>> > On 6/28/07, Robert Klemme <shortcutter@googlemail.com> wrote:

>> > But that is not what OP asked for <G>
>> >
>> > (a|[]).map{|e|a.grep(e).size} ### I know it might not work as we use
>> > #=== here
>>
>> Sorry, Robert, but I don't think this is what the OP was asking for:
>>
>> irb(main):001:0> a = %w{a a a b b c c c d d e}
>> => ["a", "a", "a", "b", "b", "c", "c", "c", "d", "d", "e"]
>> irb(main):002:0> a.map{|e|a.grep(e).size}
>> => [3, 3, 3, 2, 2, 3, 3, 3, 2, 2, 1]
> Careful here Robert I wrote
>
> (a|[]) which is a more computation intensive way to write a.uniq. --
> its a coding joke brrr

Umpf! I actually overlooked that one. Somehow I read it as (a||[]).
Duh! Sorry for the noise. Btw, that idiom was not part of my
repertoire. So I actually learned something today - although I have to
say I'd rather stick with #uniq - it's easier for my brain. :-)

Kind regards

robert