Can i calculate the data number in array?

C

Cool Wong

Code:
Array = ["a", "a", "a", "b", "b", “câ€, "c", "c","d", "d", "e"]


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"]
 
G

gz zz

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
 
G

gz zz

gz said:
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...
 
A

Axel Etzold

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
 
R

Robert Klemme

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
 
R

Robert Dober

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
 
R

Robert Klemme

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
 
R

Robert Dober

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
 
R

Robert Klemme

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top