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"]
Array = ["a", "a", "a", "b", "b", “câ€, "c", "c","d", "d", "e"]
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
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
Shame on you!!!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.
Really or are you metajoking?anyway to get out of recursive joke mode,Um, why? Somehow I don't get the joke, sorry.
Careful here Robert I wroteBut 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 wroteBut 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]
(a|[]) which is a more computation intensive way to write a.uniq. --
its a coding joke brrr
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.